【问题标题】:C# Group by list by specific element to result a list of listsC# 按特定元素按列表分组以生成列表列表
【发布时间】:2017-06-04 12:26:18
【问题描述】:

我有一个list 的对象。每个对象都有这些组件:index, countNr, descA, descB.

我如何按(可能是 LINQ)这个 listcountNr 值分组?

喜欢:

列表 = {(1,3,a1,b1), (2,2,a2,b2), (3,2,a3,b3), (4,1,a4,b4), (5,1,a5,b5), (6,1,a6,b6), (7,1,a7,b7)}

我现在想要另一个newList 列表,其中该列表中的位置表示countNr 编号,例如:

newList = {{(7,1,a7,b7), (6,1,a6,b6), (5,1,a5,b5), (4,1,a4,b4)},  {(3,2,a3,b3), (2,2,a2,b2)},  {(1,3,a1,b1)}}

【问题讨论】:

  • 请显示输入列表的有效编译示例以及您尝试过的内容(使用 linq)。按用法简单分组的例子很多
  • 也许这样:var results = myList.GroupBy(x => ((object[])x)[1]).OrderBy(x => x.Key).Select(x => x .OrderByDescending(y => ((object[])y)[0])).SelectMany(x => x).ToList();
  • 请听从 Gilad Green 的建议。

标签: c# linq group-by


【解决方案1】:

如果你的班级是这样的:

public class MyClass
{

    public MyClass(int index, int countNr, string descA, string descB)
    {
        Index = index;
        CountNr = countNr;
        DescA = descA;
        DescB = descB;
    }
    public int Index { get; set; }
    public int CountNr { get; set; }
    public string DescA { get; set; }
    public string DescB { get; set; }
}

这个输入:

var list = new List<MyClass> {
    new MyClass(1, 3, "a1", "b1"),
    new MyClass(2, 2, "a2", "b2"),
    new MyClass (3, 2, "a3", "b3"),
    new MyClass(4, 1, "a4", "b4"),
    new MyClass(5, 1, "a5", "b5"),
    new MyClass(6, 1, "a6", "b6"),
    new MyClass(7, 1, "a7", "b7") };

你可以试试:

var query = from r in list
    group r by new { r.CountNr } into grp
    orderby grp.Key.CountNr ascending
    select new
    {
        grp.Key.CountNr,
        grp
    };

那么你有这个:

foreach (var item in query)
{
    System.Diagnostics.Debug.WriteLine($"CountNr: {item.CountNr}"); //Key  or group
    //All rows of group
    foreach (var g in item.grp)
    {
        System.Diagnostics.Debug.WriteLine($"{g.Index} - {g.CountNr} - {g.DescA} - {g.DescB}");
    }                
}

这个结果:

CountNr: 1
4 - 1 - a4 - b4
5 - 1 - a5 - b5
6 - 1 - a6 - b6
7 - 1 - a7 - b7
CountNr: 2
2 - 2 - a2 - b2
3 - 2 - a3 - b3
CountNr: 3
1 - 3 - a1 - b1

如果您也需要按索引订购。试试这个:

var query = from r in list
    group r by new { r.CountNr } into grp
    orderby grp.Key.CountNr ascending
    select new
    {
        grp.Key.CountNr,
        grp =grp.OrderByDescending( c=> c.Index)
    };

你有这个结果:

CountNr: 1
7 - 1 - a7 - b7
6 - 1 - a6 - b6
5 - 1 - a5 - b5
4 - 1 - a4 - b4
CountNr: 2
3 - 2 - a3 - b3
2 - 2 - a2 - b2
CountNr: 3
1 - 3 - a1 - b1

委托人

【讨论】:

  • 请将 OP 的输入显示为有效的 C# 代码,然后显示您的代码给出的输出。
  • 输入和预期输出有问题。但已经包含在我的回复中。
  • 第一个期望值不是 7(不是 4)吗?
  • 在问题中只说按 countNr 值排序。所以我就这么做了。
  • 我编辑了我的回复,还包括按索引排序的选项。
【解决方案2】:

newList = list.OrderBy(z =&gt; z.countNr).ThenByDescending(y =&gt; y.index).ToList()

可能是你想要的。我认为它可能会根据您的示例输入生成您的示例输出。

或完整的代码示例:

class Bob
{
    public int index;
    public int countNr;
    public string descA;
    public string descB;

    public Bob(int index, int countNr, string descA, string descB)
    {
        this.index = index;
        this.countNr = countNr;
        this.descA = descA;
        this.descB = descB;
    }

    public override string ToString()
    {
        return $"{index} - {countNr} - {descA} - {descB}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        var list = new List<Bob>
        {
            new Bob(1, 3, "a1", "b1"),
            new Bob(2, 2, "a2", "b2"),
            new Bob(3, 2, "a3", "b3"),
            new Bob(4, 1, "a4", "b4"),
            new Bob(5, 1, "a5", "b5"),
            new Bob(6, 1, "a6", "b6"),
            new Bob(7, 1, "a7", "b7")
        };

        var newList = list.OrderBy(z => z.countNr).ThenByDescending(y => y.index).ToList();

        newList.ForEach(Console.WriteLine);

        Console.ReadLine();
    }
}

给出:

7 - 1 - a7 - b7
6 - 1 - a6 - b6
5 - 1 - a5 - b5
4 - 1 - a4 - b4
3 - 2 - a3 - b3
2 - 2 - a2 - b2
1 - 3 - a1 - b1

【讨论】:

  • 请将 OP 的输入显示为有效的 C# 代码,然后显示您的代码给出的输出。
  • "它将根据您的简单输入生成示例输出。" - 请给我们看。
  • 按要求完成!
  • 这不是为了我的利益。这是为了社区。这就是 SO 的意义所在。
  • 你是正确的@Enigmativity - 为蛇怪道歉。
猜你喜欢
  • 2014-02-27
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多