question

PolachanPaily-2805 avatar image
0 Votes"
PolachanPaily-2805 asked DanielZhang-MSFT commented

Groupby Linq Error in .Net framework 3.1

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


dotnet-aspnet-core-mvcdotnet-entity-framework
mapping-depot.txt (2.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT, Many Thanks for the reply , goDepartmentWorkTime.Include is not coming in the popup. Include will not be working there. Do you have any other suggestion ?

Thanks
Pol

0 Votes 0 ·

Hi @PolachanPaily-2805,
>>goDepartmentWorkTime.Include is not coming in the popup.
What pop-up window and what is the specific error?
And have you tried the AsEnumerable() I mentioned above.
Best Regards,
Daniel Zhang

0 Votes 0 ·