C#GroupByMany到列表列表

时间:2018-08-06 18:32:21

标签: c# linq

我正在尝试通过多个值来分隔一个类的列表。这是我正在使用的东西,使用汽车,年份和颜色以简化可读性的简化。

class car
{
    public string color;
    public int year;
    public int index;
}

将生成许多类并将其添加到列表中。

List<car> carList = new List<car>();

然后,我需要用多个值(颜色和年份)将列表分隔开,以获得类似的输入。

Yellow, 2008 //List.Count = 2
    Car 1
    Car 2
Yellow, 2009 //List.Count = 2
    Car 3
Red, 2012 //List.Count = 2
    Car 4
    Car 5
// queryItemsByColorYear.Count = 4

我试图做这样的事情,但是没有给出想要的输出。

var queryItemsByColorYear = carList.GroupBy(cars => new {cars.color, cars.year})
                            .Select(group => group.ToList())
                            .ToList();

或者,我认为以下内容将输出列表列表,以便像这样

进行迭代
Yellow //List.Count = 2
    2008     //List.Count = 2
        Car 1
        Car 2
    2009 //List.Count = 1
        Car 3
Red //List.Count = 1
    2012 //List.Count = 2
        Car 4
        Car 5
// queryItemsByColorAndYear.Count = 2

这是我尝试使用的...

var queryItemsByColorAndYear = from cars in carList
                                        group car by new {color = car.color, year = car.year} into colorGroup
                                        select colorGroup;

谢谢

2 个答案:

答案 0 :(得分:0)

在Linqpad中尝试。

 void Main()
    {
        var car1 = new car() { color = "Yellow", year = 2011, index =1 };
        var car2 = new car() { color = "Green", year = 2018, index =2  };
        var car3 = new car() { color = "Green", year = 2018, index =3  };
        var car4 = new car() { color = "Red", year = 2014, index =4  };
        var car5 = new car() { color = "Yellow", year = 2014, index =5  };

        List<car> carList = new List<car> {car1, car2, car3, car4, car5};

        carList.GroupBy(entry => new { entry.color, entry.year }, entry => entry.index, (a, b) => new { Key = a, Value = b }).Dump();
    }

    // Define other methods and classes here
    class car
    {
        public string color;
        public int year;
        public int index;
    }

答案 1 :(得分:0)

我为您创建了一个DotNetFiddle。这是我的代码,适合您想要的格式和属性:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var carOne = new Car()
        {color = "Yellow", year = 2008, index = 1, name = "Car One"};
        var carTwo = new Car()
        {color = "Yellow", year = 2008, index = 2, name = "Car Two"};
        var carThree = new Car()
        {color = "Yellow", year = 2009, index = 3, name = "Car Three"};
        var carFour = new Car()
        {color = "Red", year = 2012, index = 4, name = "Car Four"};
        var carFive = new Car()
        {color = "Red", year = 2012, index = 5, name = "Car Five"};

        var lstCars = new List<Car>() {carOne, carTwo, carThree, carFour, carFive};


        var groupCars = lstCars.GroupBy(x => new {x.color, x.year}).Select(t => new{Color = t.Key.color, Year = t.Key.year, ListCount = t.Count(), Names = t.Select(z => z.name).ToList()}).ToList();


        foreach (var item in groupCars)
        {
            Console.WriteLine("{0} \t List Count - {1}", item.Color, item.ListCount);
            Console.WriteLine("\t{0}", item.Year);
            foreach (var name in item.Names)
            {
                Console.WriteLine("\t\t" + name);
            }

            Console.WriteLine("\n");
        }
    }
}

public class Car
{
    public string color;
    public int year;
    public int index;
    public string name;
}

让我知道这是否有帮助。