LINQ - 如何对不同的值进行分组和计数?

时间:2017-05-12 10:27:08

标签: vb.net linq

我正在尝试对列表进行一些分组和聚合。我需要找出一种聚合输出集合中特定值的实例数的方法。例如......

我将从一系列汽车开始

    Private Class Car
        Property Colour As String
        Property Make As String
        Property Doors As Integer
    End Class

    Dim cars = New List(Of Car) _
                From {New Car With {.Colour = "Blue", .Make = "Ford", .Doors = 5},
                        New Car With {.Colour = "Red", .Make = "Ford", .Doors = 2},
                        New Car With {.Colour = "Red", .Make = "Pontiac", .Doors = 3},
                        New Car With {.Colour = "Green", .Make = "Nissan", .Doors = 3},
                        New Car With {.Colour = "Blue", .Make = "Ford", .Doors = 5},
                        New Car With {.Colour = "Blue", .Make = "Pontiac", .Doors = 4},
                        New Car With {.Colour = "Green", .Make = "Nissan", .Doors = 5},
                        New Car With {.Colour = "Blue", .Make = "Volkswagen", .Doors = 5},
                        New Car With {.Colour = "Green", .Make = "Pontiac", .Doors = 3},
                        New Car With {.Colour = "Blue", .Make = "Nissan", .Doors = 2},
                        New Car With {.Colour = "Red", .Make = "Volkswagen", .Doors = 5},
                        New Car With {.Colour = "Red", .Make = "Ford", .Doors = 5}}

然后我们可以写一个linq查询...

    Dim groupedList = (From c In cars
                        Group By c.Make Into Group
                        Select New With {.Make = Make,
                                        .Cars = Group.Count(),
                                        .TotalDoors = Group.Sum(Function(x) x.Doors)}).ToList()

这将给我们以下结果:

  • 制作福特汽车 4 TotalDoors 17
  • 制作庞蒂亚克汽车 3 TotalDoors 10
  • 制作日产汽车 3 TotalDoors 10
  • 制作 VolksWagen 汽车 2 TotalDoors 10

很棒的东西!我们现在知道这些优质制造商提供的车门总数和汽车总数。接下来我需要知道他们做的不同颜色的数量,我只是这样做:

    Dim groupedList = (From c In cars
                        Group By c.Make Into Group
                        Select New With {.Make = Make,
                                    .Cars = Group.Count(),
                                    .Colours = Group.Count(Function(x) x.Colour),
                                    .TotalDoors = Group.Sum(Function(x) x.Doors)}).ToList()

我希望在我的每个列表项目上看到另一个名为Color的属性以及它们提供的不同颜色的数量......

  • 制作福特汽车 4 TotalDoors 17 颜色 1
  • 制作庞蒂亚克汽车 3 TotalDoors 10 颜色 2
  • 制作日产汽车 3 TotalDoors 10 颜色 2
  • 制作 VolksWagen 汽车 2 TotalDoors 10 颜色 2

但我不,我只是犯了一个很大的错误,

Run-time exception (line -1): Conversion from string "Blue" to type 'Boolean' is not valid.

我不确定在互联网上寻找什么,我很确定答案应该很简单。

这是一个.net小提琴玩: .Net Fiddle for this question

2 个答案:

答案 0 :(得分:2)

您也可以使用select distinct

.Colours = Group.Select(Function(x) x.Colour).Distinct.Count

答案 1 :(得分:0)

您按照颜色组合分组结果:

 Dim groupedList = (From c In cars
                           Group By c.Make Into Group
                           Select New With {.Make = Make,
                            .Cars = Group.Count(),
                            .Colours = Group.GroupBy(Function(x) x.Colour).Count(),
                            .TotalDoors = Group.Sum(Function(x) x.Doors)}).ToList()

更新了fiddle