Group by inner list parameter to filter outer list

2019-06-11 20:54发布

I am new to writing queries to fetch data from List of objects. I am having below class structure


    public class Locations
    {
        public string id { get; set; }
        public string name { get; set; }
        public List model { get; set; }
        public List sectors { get; set; }
    }

    public class Sector
    {
        public string sectoreid { get; set; }
        public string sectorname { get; set; }
        public List streams { get; set; }
        public List employees { get; set; }
    }   

    public class Stream
    {
        public string streamid { get; set; }
        public string streamname { get; set; }
        public string geographyid { get; set; }
        public string geographyname { get; set; }
        public string countryid { get; set; }
        public string countryname { get; set; }
    }

    public class Employee
    {
        public string id { get; set; }
        public string name { get; set; }
        public string code { get; set; }
    }

I am getting the list of Locations and I want to group by the list so that I can have result like I want to group this collections in such a way that I can get hierarchy like Geography -> Country -> Stream -> Employee -> Sector -> Locations

Also want it to type cast to a specific class like


    public class Tree
    {
        public string id { get; set; }
        public string name { get; set; }
        public List nodes{ get; set; }
    }

I tried below query


siteList.SelectMany(a => a.streams.Select(b => new { A = a, B = b }).ToList()).ToList()
                         .GroupBy(ol => new { ol.B.geographyid, ol.B.geographyname })
                         .Select(gGroup => new Tree
                         {
                             id = gGroup.Key.geographyid,
                             name = gGroup.Key.geographyname,
                             children = gGroup
                                 .GroupBy(ol => new { ol.B.countryid, ol.B.countryname })
                                 .Select(cGroup => new Tree
                                 {
                                     id = cGroup.Key.countryid,
                                     name = cGroup.Key.countryname,
                                     children = cGroup
                                       .GroupBy(ol => new { ol.B.id, ol.B.name })
                                       .Select(sGroup => new Tree
                                       {
                                           id = sGroup.Key.id,
                                           name = sGroup.Key.name,
                                           children = sGroup
                                            .Select(ol => new Trees { id = ol.A.id, name = ol.A.name, children = new List() })
                                            .ToList()
                                       })
                                      .ToList()
                                 })
                                 .ToList()
                         })
                         .ToList();

标签: c# linq lambda
0条回答
登录 后发表回答