LINQ Group by 多列值在C#與VB.Net上寫法的區別
• net教程• 發佈:2018-10-08
現有這樣一個LIST,需要根據Age和Sex兩列進行Group by 分組操作:
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'}
};
用C# 很好實現,一般這樣來寫LINQ語句:
// query with lamda expression
var QueryWithLamda = empList.GroupBy(x => new { x.Age, x.Sex})
.Select(g=>new {g.Key, Count=g.Count()});
//query with standard expression
var query=from el in empList
group el by new {el.Age,el.Sex} into g
select new {g.Key, Count=g.Count()};
現在需要用VB.Net來實現,根據上邊C#的寫法,很容易想到這樣來寫:
' query with lamda expression
Dim QueryWithLamda = empList.GroupBy(Function(x) New With { .Age=x.Age, .Sex= x.Sex}) _
.Select(Function(g) New With {g.Key, g.Count()})
' query with standard expression
Dim Query = From el In empList _
Group el By Key = new with { el.Age, el.Sex} Into g= Group _
Select New With {.key = Key, _
.count = g.Count()}
但是列印一下結果發現不對,後來仔細研究了下,原來問題出在這:
Group el By Key = new with {el.Age, el.Sex} Into g= Group _
換成這樣,結果就和C#的一樣了:
Group el By Key = new with{key el.Age, key el.Sex} Into g= Group _
看來在VB.NET中,匿名類和C#不一樣,當Group by多列時,需要全部指定它們為Key,不然不會將它們作為一個整體進行group by
以下是全部程式碼:
C#:
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'}
};
// query with lamda expression
var QueryWithLamda = empList.GroupBy(x => new { x.Age, x.Sex})
.Select(g=>new {g.Key, Count=g.Count()});
//query with standard expression
var query=from el in empList
group el by new {el.Age,el.Sex} into g
select new {g.Key, Count=g.Count()};
foreach (var employee in query /* Or QueryWithLamda */ )
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;}
}
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})
' query with lamda expression
Dim QueryWithLamda = empList.GroupBy(Function(x) New With { Key x.Age, Key x.Sex}) _
.Select(Function(g) New With {g.Key, g.Count()})
' query with standard expression
Dim QueryWithStanard = From el In empList _
Group el By Key = new with {Key el.Age, Key el.Sex} Into g= Group _
Select New With {.key = Key, _
.count = g.Count()}
Dim QueryWithStanard2 = From el In empList _
Group el By Key = el.Age, el.Sex Into g= Group _
Select New With {.key = Key, _
.count = g.Count()}
For Each employee In QueryWithLamda 'Or QueryWithLamda
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
相關講討論討論帖子:
http://topic.csdn.net/u/20120529/16/2648950b-26b4-4b90-aa9a-05dff88c85f4.html
http://stackoverflow.com/questions/10801859/linq-group-by-multiple-values-does-not-work-well-in-vb-net-but-work-well-in-c-sh
http://social.microsoft.com/Forums/zh-CN/adonetzhchs/thread/7f06ff9e-a704-411e-9220-7a86963a290e
