Groupby Linq Error in .Net framework 3.1

Polachan Paily 226 Reputation points
2021-06-14T15:03:10.023+00:00

I am struggling to convert a linq expression from .Net framework 2.1 into 3.1. Linq Expression is not working when I add subs = g.select...

public JsonResult GetDepotDepartemntsForMap()  
 {  
    dynamic mappingList = new List<DepotMapModel>();  
            mappingList = _unitOfWork.Department.GetDepotWithDepartment();           
            return Json(mappingList);  
 }  
    public dynamic GetDepotWithDepartment()  
            {  
                var list = goContext.goDepartmentWorkTime.  
                    GroupBy(d => new { d.DepotNo, d.Depot.DepotName })  
                .Select(g => new  
                {  
                    id = g.Key.DepotNo,  
                    title = g.Key.DepotName,  
                    subs = g.Select(dd => new  
                    {  
                        id = dd.DepotNo + "." + dd.DepartmentID,  
                        title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName  
                    }).ToList()  
                }).ToList();  
                  
                return list;  
            }  

I have attached herewith my model class in a text file herewith
105481-mapping-depot.txt

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-06-15T02:33:47.87+00:00

    Hi PolachanPaily-2805,
    First, client side GroupBy is not supported in .netcore 3.1.
    You need to grab the data first and processed later with this LINQ query.
    So you can change your query as below:

    var t=goContext.goDepartmentWorkTime.Include(x=>x.Depot).Include(x=>x.Department).ToList().GroupBy(d => new { d.DepotNo, d.Depot.DepotName })  
                .Select(g => new  
                {  
                    id = g.Key.DepotNo,  
                    title = g.Key.DepotName,  
                    subs = g.Select(dd => new  
                    {  
                        id = dd.DepotNo + "." + dd.DepartmentID,  
                        title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName  
                    }).ToList()  
                }).ToList();  
    

    You can also try to use AsEnumerable(), more details please refer to this thread.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.