public dynamic GetDepotWithDepartment()
{
var list = _context.DepartmentWorkTimes.
GroupBy(d => new { d.Depot.Id, d.Depot.DepotName })
.Select(g => new
{
id = g.Key.Id,
title = g.Key.DepotName,
subs = g.Select(dd => new
{
id = dd.Id + "." + dd.DepartmentID,
title = dd.Depot.Id + "." + dd.Department.DepartmentName
}).ToList()
}).ToList();
return list;
}
And you could call it like below:
dynamic mappingList = new List<DepotMapModel>();
mappingList = GetDepotWithDepartment();
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Contributor
2720 Points
874 Posts
Re: How can I pass the group by Linq query into a List<model>
Jul 29, 2020 06:58 AM|Rena Ni|LINK
Hi polachan,
Change like below:
public dynamic GetDepotWithDepartment() { var list = _context.DepartmentWorkTimes. GroupBy(d => new { d.Depot.Id, d.Depot.DepotName }) .Select(g => new { id = g.Key.Id, title = g.Key.DepotName, subs = g.Select(dd => new { id = dd.Id + "." + dd.DepartmentID, title = dd.Depot.Id + "." + dd.Department.DepartmentName }).ToList() }).ToList(); return list; }
And you could call it like below:
dynamic mappingList = new List<DepotMapModel>(); mappingList = GetDepotWithDepartment();
Best Regards,
Rena