Monday, 8 February 2021

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

 Watch this example on YouTube


to fix this error replace

        public JsonResult BarChartData()
        {
            var data = db.SelectChartTest();

            Chart _chart = new Chart();
            _chart.labels = data.Select(x => x.Month).ToArray();
            _chart.datasets = new List<Datasets>();
            List<Datasets> _dataSet = new List<Datasets>();
            _dataSet.Add(new Datasets()
            {
                label = "This Year",
                data = data.Select(x => x.Amount.Value).ToArray(),
                backgroundColor = new string[] { "800000", "#E9967C", "#FF0000" },
                borderColor = new string[] { "800000", "#E9967C", "#FF0000" },
                borderWidth = "1"
            });
            _chart.datasets = _dataSet;
            return Json(_chart, JsonRequestBehavior.AllowGet);
        }

with

       public JsonResult BarChartData()
        {
            var data = db.SelectChartTest().ToList();

            Chart _chart = new Chart();
            _chart.labels = data.Select(x => x.Month).ToArray();
            _chart.datasets = new List<Datasets>();
            List<Datasets> _dataSet = new List<Datasets>();
            _dataSet.Add(new Datasets()
            {
                label = "This Year",
                data = data.Select(x => x.Amount.Value).ToArray(),
                backgroundColor = new string[] { "800000", "#E9967C", "#FF0000" },
                borderColor = new string[] { "800000", "#E9967C", "#FF0000" },
                borderWidth = "1"
            });
            _chart.datasets = _dataSet;
            return Json(_chart, JsonRequestBehavior.AllowGet);
        }

 

No comments:

Post a Comment