CS0266 error Cannot convert implicitly

Asked

Viewed 751 times

1

When trying to make a list where you will filter only the 5 largest records by values in descending order I am experiencing this error:

CS0266 error Cannot convert type implicitly "System.Linq.Iqueryable" in "System.Collections.Generic.Icollection". There is an explicit conversion (there is an absent conversion?)

Here is my code:

public class DashboardController : Controller
    {
        private NFeWebContext db = new NFeWebContext();
        // GET: Dashboard
        public ActionResult Index()
        {
            var dashboard = new DashboardViewModel();
            var participantes = db.Participantes
                          .ToList();
            var notas = db.NotasFiscais
                          .ToList();
            var lista = db.NotasFiscais
                  .Where(x => db.Participantes.Any(y => y.ParticipanteId == x.ClienteID))
                  .GroupBy(z => z.ClienteID)
                  .Select(x => new NotaFiscal()
                  {
                      ClienteID = x.First().ClienteID,
                      ValorTotalNota = x.Sum(n => n.ValorTotalNota)
                  })
                  .OrderByDescending(x => x.ValorTotalNota)
                  .Take(5);

            dashboard.NotasFiscais = lista;
            return View(dashboard);
        }
    }

1 answer

0


Playing with LINQ without understanding it well doesn’t usually go well. This code has some weird things, but the biggest problem is that the query is not being materialized. It would be like this:

dashboard.NotasFiscais = lista.ToList();

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.