首页新闻找找看学习计划

如何使用 Linq 对list数组进行分组?同时有多个字段

0
悬赏园豆:10 [已解决问题] 解决于 2019-12-02 17:35

数据:

G1 | TINA AM | 22 | CT | 3
G2 | TINA AM | 22 | TLC | 3
G3 | TINA AM | 22 | TLC | 3
G4 | TINA AM | 10 | CT | 2

想要实现的效果:

list1:
G1 | TINA AM | 22 | CT | 3

list2:
G2 | TINA AM | 22 | TLC | 3
G3 | TINA AM | 22 | TLC | 3

list3:
G4 | TINA AM | 10 | CT | 2

部分代码如下:

  public class CommodityInfo
    {
        public string GateNo { get; set; }
        public string NozzleStyle { get; set; }
        public string NozzleSize { get; set; }
        public string NozzleType { get; set; }
        public int HeatreCount { get; set; }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        List<CommodityInfo> personList = new List<CommodityInfo>();

        personList.Add(new CommodityInfo() { GateNo = "G1", NozzleStyle = "TINA AM", NozzleSize = "22", NozzleType = "CT", HeatreCount = 3 });
        personList.Add(new CommodityInfo() { GateNo = "G2", NozzleStyle = "TINA AM", NozzleSize = "22", NozzleType = "TLC", HeatreCount = 3 });
        personList.Add(new CommodityInfo() { GateNo = "G3", NozzleStyle = "TINA AM", NozzleSize = "22", NozzleType = "TLC", HeatreCount = 3 });
        personList.Add(new CommodityInfo() { GateNo = "G4", NozzleStyle = "TINA AM", NozzleSize = "10", NozzleType = "CT", HeatreCount = 2 });

    }
guitarboy的主页 guitarboy | 初学一级 | 园豆:190
提问于:2019-11-21 15:16
< >
分享
最佳答案
0

personList.GroupBy(t=>$"{t.NozzleType}{t.HeatreCount }" )
.Dictionary(t=>t.Key,t=>t.ToArray();)

收获园豆:8
通信的搞程序 | 小虾三级 |园豆:893 | 2019-11-21 16:43

问题已解决,谢谢🙏

guitarboy | 园豆:190 (初学一级) | 2019-11-26 08:12
其他回答(2)
0

看你这例子也不是分组啊,不是筛选吗?
姑且看成分组,比如按照NozzleSize 和NozzleType 分组,

var groupNozzleSize = personList.groupby(new{param1=NozzleSize ,param2=NozzleType});

收获园豆:1
猝不及防 | 园豆:1695 (小虾三级) | 2019-11-21 16:46

感谢回复,问题已解决🙏

支持(0) 反对(0) guitarboy | 园豆:190 (初学一级) | 2019-11-26 08:13
0

var list1=personList.where(e=>e.NozzleType.Equals("CT")&&e.HeatreCount==3 );
var list2=personList.where(e=>e.NozzleType.Equals("TLC")&&e.HeatreCount==3 );
var list3=personList.where(e=>e.NozzleType.Equals("CT")&&e.HeatreCount==2 );

收获园豆:1
wwr01 | 园豆:593 (小虾三级) | 2019-11-21 16:58

感谢回复,问题已解决🙏

支持(0) 反对(0) guitarboy | 园豆:190 (初学一级) | 2019-11-26 08:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册