c#对元组列表进行分组然后排序<T1,T2,T3> [英] c# Group and then Sort list of tuple <T1,T2,T3>
本文介绍了c#对元组列表进行分组然后排序<T1,T2,T3>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 C# 新手,我想对我的 List
I'm new to C# and I want to sort my collection of 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
我希望排序列表看起来像;
I would like the sort list to look like;
ID = 1 { abbc,3|becky,5|betty,56 }
ID = 2 { Olivia,6|abbc,3|becky,5 }
ID = 3 { Olivia,675 }
请帮助我真的卡住了:)
please help i'm really stuck :)
推荐答案
看起来你更像是想要Group
你的项目然后Order
按第一个 int (Id
):
It looks more like you want to Group
your items and then Order
by the first int (Id
):
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#对元组列表进行分组然后排序<T1,T2,T3>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文