我有一个字符串元组列表,比如说(P1,P2)
我想知道是否有一个LINQ语句,我可以按P1(按升序排列)进行分组,并让该组包含该组的所有P2值(按降序排列)。
输入:(“A”,“B”),(“A”,“C”),(“D”,“B”) 我想得到两组:“A”和“D”(按此顺序,每次) 组“A”包含“C”和“B”(按此顺序,每次),组“D”包含“B”。
这是否可以使用内置的LINQ类,或者我是否需要迭代这些组并自行排序?
答案 0 :(得分:20)
不,这并不难 - 您只需要跟踪您是否正在查看组内的组或元素。这是一个示例查询:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
这是一个完整的示例(为了简单起见,如果您使用的是.NET 3.5,请避免使用.NET 4的Tuple
类型):
using System;
using System.Collections.Generic;
using System.Linq;
public class MyTuple
{
public string P1 { get; set; }
public string P2 { get; set; }
}
class Test
{
static void Main()
{
List<MyTuple> tuples = new List<MyTuple>
{
new MyTuple { P1 = "A", P2 = "B" },
new MyTuple { P1 = "A", P2 = "C" },
new MyTuple { P1 = "D", P2 = "B" },
};
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1 into g
select new { Group = g.Key,
Elements = g.OrderByDescending(p2 => p2) };
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Group);
foreach (var value in group.Elements)
{
Console.WriteLine(" {0}", value);
}
}
}
}
请注意,如果您乐意在“需要知道”的基础上进行订购,可能会稍微简单一些:
var query = from tuple in tuples
orderby tuple.P1
group tuple.P2 by tuple.P1;
foreach (var group in query)
{
Console.WriteLine("{0}:", group.Key);
foreach (var value in group.OrderByDescending(x => x))
{
Console.WriteLine(" {0}", value);
}
}
答案 1 :(得分:4)
List<Tuple<string, string>> tuples = //
var grouped = tuples.GroupBy(t => t.First)
.OrderBy(grp => grp.Key)
.Select(grp => new { Key = grp.Key, Items = grp.OrderBy(t => t.Second) });