现有这样一个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
- 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://social.microsoft.com/Forums/zh-CN/adonetzhchs/thread/7f06ff9e-a704-411e-9220-7a86963a290e
本文详细介绍了如何在VB.NET中使用LINQ语法实现与C#相似的Group By多列操作,特别关注了匿名类在不同语言版本中的差异,并提供了完整的代码示例和对比分析。
2225

被折叠的 条评论
为什么被折叠?



