public class DepartmentMapModel
{
public string id { get; set; }
public string title { get; set; }
}
public class DepotMapModel
{
public string id { get; set; }
public string title { get; set; }
public List<DepartmentMapModel> subs { get; set; }
}
In repository class I am using the following Linq Query to get the record. How can I pass the output result from the query in to List<DepotMapModel> in Repository class
//Repository class
public List<DepotMapModel> GetDepotWithDepartment()
{
var list = goContext.goDepartmentWorkTime.
GroupBy(d => new { d.DepotID, d.Depot.DepotName })
.Select(g => new
{
id = g.Key.DepotID,
title = g.Key.DepotName,
subs = g.Select(dd => new
{
id = dd.DepotID + "." + dd.DepartmentID,
title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
})
});
rerturn DepotMapModel
//In Controller class I would like to call the function from Repository
List<DepotMapModel> mappingList= new List<DepotMapModel>();
mappingList = GetDepotWithDepartment()
In repository class I am using the following Linq Query to get the record. How can I pass the output result from the query in to List<DepotMapModel> in Repository class
DepotMapModel is a custom class/type, and therefore, you would have to use a Linq projection using the custom type of DepotMapModel .
Right now, you're projecting out an anaymous type object and not the DepotMapModel custom type object.
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.
Member
410 Points
1326 Posts
How can I pass the group by Linq query into a List<model>
Jul 28, 2020 09:43 AM|polachan|LINK
I have following two model class
In repository class I am using the following Linq Query to get the record. How can I pass the output result from the query in to List<DepotMapModel> in Repository class
Any help would be appreciated
Member
30 Points
32 Posts
Re: How can I pass the group by Linq query into a List<model>
Jul 28, 2020 01:25 PM|vsetia|LINK
It might help :)
Contributor
4923 Points
4198 Posts
Re: How can I pass the group by Linq query into a List<model>
Jul 28, 2020 04:36 PM|DA924|LINK
In repository class I am using the following Linq Query to get the record. How can I pass the output result from the query in to List<DepotMapModel> in Repository class
DepotMapModel is a custom class/type, and therefore, you would have to use a Linq projection using the custom type of DepotMapModel .
Right now, you're projecting out an anaymous type object and not the DepotMapModel custom type object.
http://csharp-station.com/Tutorial/Linq/Lesson02
Contributor
2690 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
Member
410 Points
1326 Posts
Re: How can I pass the group by Linq query into a List<model>
Jul 29, 2020 08:31 AM|polachan|LINK
Absolutely brilliant Many Thanks