Tuesday, 16 February 2021

MVC - Fix Error - The result of a query cannot be enumerated more than once.

 Watch this example on YouTube


 

To fix it replace

         public ActionResult Index()
        {
            OrderCustomClass model = new OrderCustomClass();
            model.prod = db.SelectProducts();
            int count = model.prod.Count();
            foreach(var item in model.prod)

            {
                var test = item.Name;
            }
            return View(model);
        }

with

        public ActionResult Index()
        {
            OrderCustomClass model = new OrderCustomClass();
            model.prod = db.SelectProducts();
            var vr = (from s in model.prod select s);
            vr = vr.ToList();
            int count = vr.Count();

            foreach(var item in vr)

            {
                var test = item.Name;
            }
            return View(model);
        }

 

No comments:

Post a Comment