C#按特定元素按列表分组以生成列表列表

时间:2017-06-04 12:26:19

标签: c# linq group-by

我有list个对象。每个对象都包含以下组件:index, countNr, descA, descB.

我如何按list countNr分组(可能是LINQ)?

像:

列表= {(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)}}

2 个答案:

答案 0 :(得分:2)

如果您的课程如下:

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

ATT。

答案 1 :(得分:1)

newList = list.OrderBy(z => z.countNr).ThenByDescending(y => 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