Csharp/C#教程:按内部列表参数分组以过滤外部列表数据分享


按内部列表参数分组以过滤外部列表数据

我是新手编写查询以从对象列表中获取数据。 我有以下阶级结构

 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; } } 

我正在获取位置列表,我想按列表分组,以便我可以得到结果,就像我想以这样的方式对这个集合进行分组,以便我可以获得像Geography – > Country – > Stream – > Employee – >这样的层次结构。部门 – >地点

也希望它类型转换为特定类

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

我试过以下查询

上述就是C#学习教程:按内部列表参数分组以过滤外部列表数据分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/985567.html

(0)
上一篇 2021年12月21日
下一篇 2021年12月21日

精彩推荐