gpt4 book ai didi

c# Group 然后排序元组列表

转载 作者:行者123 更新时间:2023-11-30 13:54:50 25 4
gpt4 key购买 nike

我是 C# 的新手,我想对我的 List<Tuple<int, string, int>> 集合进行排序

用户输入数据例如:

T1   T2   T3
1 abbc 3
1 becky 5
1 betty 56
2 Olivia 6
2 abbc 3
2 becky 5
3 Olivia 675

我希望排序列表看起来像这样;

 ID = 1 { abbc,3|becky,5|betty,56 }        
ID = 2 { Olivia,6|abbc,3|becky,5 }
ID = 3 { Olivia,675 }

请帮助我真的卡住了:)

最佳答案

它看起来更像是你想Group你的项目,然后通过第一个int(Id)Order:

List<Tuple<int, string, int>> data = new List<Tuple<int, string, int>>
{
new Tuple<int, string, int>(1,"abbc",3),
new Tuple<int, string, int>(1,"becky",5),
new Tuple<int, string, int>(1,"betty",56),
new Tuple<int, string, int>(2,"Olivia",6),
new Tuple<int, string, int>(2,"abbc",3),
new Tuple<int, string, int>(2,"becky",5),
new Tuple<int, string, int>(3,"Olivia",675),
};

var result = data.GroupBy(item => item.Item1)
.Select(group => new { Id = group.Key, Values = group.Select(item => new { item.Item2, item.Item3 }).ToList() })
.OrderBy(item => item.Id).ToList();

关于c# Group 然后排序元组列表 <T1,T2,T3>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com