locked
How can I pass the group by Linq query into a List<model> RRS feed

  • Question

  • User-1355965324 posted

    I have following two model class  

     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()
    

    Any help would be appreciated

    Tuesday, July 28, 2020 9:43 AM

Answers

  • User711641945 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 6:58 AM

All replies

  • User-2121988648 posted


    //Repository class
    public List<DepotMapModel> GetDepotWithDepartment() { var list = goContext.goDepartmentWorkTime. GroupBy(d => new { d.DepotID, d.Depot.DepotName }) .Select(g => new DepotMapModel { //.. Set Values }).ToList(); return list;
    }

    It might help :)

    Tuesday, July 28, 2020 1:25 PM
  • User1120430333 posted

    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

    Tuesday, July 28, 2020 4:36 PM
  • User711641945 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 29, 2020 6:58 AM
  • User-1355965324 posted

    Absolutely brilliant Many Thanks 

    Wednesday, July 29, 2020 8:31 AM