在 LINQ 中排序和分组

发布于 2024-08-30 16:08:31 字数 253 浏览 1 评论 0原文

我有一个字符串元组列表,比如 (P1,P2)

我想知道是否有一个 LINQ 语句,我可以按 P1 进行分组(按升序排列),并让该组包含该组的所有 P2 值(按降序排列)。

对于输入:(“A”,“B”),(“A”,“C”),(“D”,“B”) 我想要两组:“A”和“D”(每次都按这个顺序) 其中“A”组包含“C”和“B”(每次都按此顺序),“D”组包含“B”。

这可以通过内置 LINQ 类实现吗?或者我是否需要迭代这些组并自己对它们进行排序?

I have a list of string tuples, say (P1,P2)

I'd like to know if there's a LINQ statement where I could group by P1 (in ascending order), and have that group contain all the P2 values for the group (in descending order).

For input: ("A","B"), ("A","C"), ("D","B")
I'd like to get two groups: "A" and "D" (in that order, every time)
where group "A" contains "C" and "B" (in that order, every time) and group "D" contains, well, "B".

Is this possible with the built-in LINQ classes or do I need to iterate the groups and sort them myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼窥荷 2024-09-06 16:08:31

不,这并不难 - 您只需要跟踪您是否正在查看组或组内的元素。这是一个示例查询:

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);
    }
}

Nope, it's not hard - you just need to keep track of whether you're looking at a group or elements within the group. Here's a sample query:

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) };

Here's a complete example (avoiding .NET 4's Tuple type just for simplicity if you're using .NET 3.5):

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);
            }
        }
    }
}

Note that it can be slightly simpler if you're happy to do the ordering on a "need to know" basis:

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);
    }
}
樱娆 2024-09-06 16:08:31
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) });
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) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文