我是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 }
请帮助我真的卡住了:)
答案 0 :(得分:6)
看起来你想要Group
你的项目然后Order
更喜欢第一个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();