Error CS0266 Can not convert implicitly

1

When trying to make a listing where I only filter the 5 largest records by values in descending order this error is occurring:

  

Error CS0266 Can not implicitly convert type   "System.Linq.IQueryable" in   "System.Collections.Generic.ICollection".   Is there an explicit conversion (is there a missing 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);
        }
    }
    
asked by anonymous 25.05.2018 / 15:03

1 answer

1

Playing with LINQ without understanding it well does not usually give a good thing. This code has some weird things, but the biggest problem is that query is not being materialized. It would look like this:

dashboard.NotasFiscais = lista.ToList();
    
25.05.2018 / 15:33