LINQ 按多个值分组在 VB.NET 中效果不佳,但在 C# 中效果很好

本文关键字:很好 但在 NET VB LINQ | 更新日期: 2023-09-27 18:35:49

我发现当按多个值分组时,VB.NET 不能很好地工作,但它在 C# 上效果很好,这是我的代码,我的 VB.NET 代码有问题吗?

这是我的 VB.NET 代码:

Sub Main
  Dim empList As New List(Of Employee)()
    empList.Add(New Employee() With _
    {.ID = 1, .FName = "John", .Age = 23, .Sex = "M"c}) 
    empList.Add(New Employee() With _
    {.ID = 2, .FName = "Mary", .Age = 25, .Sex = "F"c})
    empList.Add(New Employee() With _
    {.ID = 3, .FName = "Amber", .Age = 23, .Sex = "M"c}) 
    empList.Add(New Employee() With _
    {.ID = 4, .FName = "Kathy", .Age = 25, .Sex = "F"c})
    empList.Add(New Employee() With _
    {.ID = 5, .FName = "Lena", .Age = 27, .Sex = "F"c}) 
    empList.Add(New Employee() With _
    {.ID = 6, .FName = "Bill", .Age = 28, .Sex = "M"c})
    empList.Add(New Employee() With _
    {.ID = 7, .FName = "Celina", .Age = 27, .Sex = "F"c}) 
    empList.Add(New Employee() With _
    {.ID = 8, .FName = "John", .Age = 28, .Sex = "M"c})
Dim query = empList.GroupBy(Function(x)  New With { .Age=x.Age, .Sex= x.Sex}) _
            .Select(Function(g) New With {g.Key, g.Count()}) 
For Each employee In query 
         Console.WriteLine(employee.Count)
Next employee 
End Sub

Public Class Employee 
    Private privateID As Integer
    Public Property ID() As Integer 
        Get 
            Return privateID
        End Get 
        Set(ByVal value As Integer) 
            privateID = value
        End Set 
    End Property 
    Private privateFName As String 
    Public Property FName() As String
        Get 
            Return privateFName
        End Get 
        Set(ByVal value As String) 
            privateFName = value
        End Set 
    End Property  
    Private privateAge As Integer 
    Public Property Age() As Integer
        Get 
            Return privateAge
        End Get 
        Set(ByVal value As Integer) 
            privateAge = value
        End Set 
    End Property  
    Private privateSex As Char 
    Public Property Sex() As Char
        Get 
            Return privateSex
        End Get 
        Set(ByVal value As Char) 
            privateSex = value
        End Set 
    End Property
End Class 

员工的输出。计数都是1,这是错误的。我尝试用C#,它运行良好,结果是正确的。

//Here are my C# code:
void Main()
{
 var empList =new List<Employee>
 {
    new Employee {ID = 1, FName = "John", Age = 23, Sex = 'M'},
    new Employee {ID = 2, FName = "Mary", Age = 25, Sex = 'F'},
    new Employee {ID = 3, FName = "Amber", Age = 23, Sex = 'M'},
    new Employee {ID = 4, FName = "Kathy", Age = 25, Sex = 'F'},
    new Employee {ID = 5, FName = "Lena", Age = 27, Sex = 'F'},
    new Employee {ID = 6, FName = "Bill", Age = 28, Sex = 'M'},
    new Employee {ID = 7, FName = "Celina", Age = 27, Sex = 'F'},
    new Employee {ID = 8, FName = "John", Age = 28, Sex = 'M'} 
 };
var query = empList.GroupBy(x => new { x.Age,  x.Sex})
                   .Select(g=>new {g.Key, Count=g.Count()}); 
foreach (var employee in query )
   Console.WriteLine(employee.Count);
 }

public class Employee
{
  public int ID {get;set;}
  public string FName {get;set;}
  public int Age {get;set;}
  public char Sex {get;set;}
 }

LINQ 按多个值分组在 VB.NET 中效果不佳,但在 C# 中效果很好

此查询应该有效:

Dim query =
    From el In empList
    Group el By Key = new with {key el.Age, key el.Sex} 
        Into Group
    Select New  With {.key = Key,
                      .count = Group.Count()}

你错过了关键字

下面是 lambda 表达式查询

Dim query = empList.GroupBy(Function(x)  New With { Key .Age=x.Age, Key .Sex= x.Sex}) _
                   .Select(Function(g) New With {.KeyName = g.Key, .Count = g.Count()}) 

作为参考,In VB.Net 您还可以像这样编写查询:

Dim query = From e In empList
            Group e By e.Age, e.Sex Into Group.Count()
For Each item In query
    Console.WriteLine(e.Count)
Next

其中"item"是具有"年龄"、"性别"和"计数"属性的匿名类型。

相关文章: