c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

Convert LINQ Query results to List Object

| | ASP-NET , CSharp , Linq

You can convert the entity object to list Object by looping the LINQ Query results and adding the object to the list.

The following example code clears how to convert to List Object

public ActionResult GetChart()
        {
            List<object>chartData = new List<object>();
            chartData.Add(new object[]{
                "ShipCity", "TotalOrders"
            });

            var result = (from order in db.Orders.AsEnumerable()
                          group order by order.ShipCity into rowGroup
                          select new
                          {
                              ShipCity =rowGroup.Key,
                             TotalOrders =rowGroup.Count()
                          }).Distinct().ToList();

            foreach (var c in result)
            {
               chartData.Add(new object[]
                   {
                       c.ShipCity, c.TotalOrders
                   });
            }
             return new JsonResult
            {
               Data = new
               {
                   success = chartData,
                   message = "Success",
               },
               JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
       }

Output: