c#组然后排序元组列表<t1,t2,t3>

时间:2016-08-12 10:45:05

标签: c# linq list sorting tuples

我是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 }

请帮助我真的卡住了:)

1 个答案:

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