LINQ Group by 多列值在C#与VB.Net上写法的区别

现有这样一个LIST,需要根据Age和Sex两列进行Group by 分组操作:

 

[csharp] view plain copy
  1. var  empList =new List<Employee>  
  2. {  
  3. new Employee {ID = 1, FName = "John", Age = 23, Sex = 'M'},  
  4. new Employee {ID = 2, FName = "Mary", Age = 25, Sex = 'F'},  
  5. new Employee {ID = 3, FName = "Amber", Age = 23, Sex = 'M'},  
  6. new Employee {ID = 4, FName = "Kathy", Age = 25, Sex = 'F'},  
  7. new Employee {ID = 5, FName = "Lena", Age = 27, Sex = 'F'},  
  8. new Employee {ID = 6, FName = "Bill", Age = 28, Sex = 'M'},  
  9. new Employee {ID = 7, FName = "Celina", Age = 27, Sex = 'F'},  
  10. new Employee {ID = 8, FName = "John", Age = 28, Sex = 'M'}   
  11. };  



用C# 很好实现,一般这样来写LINQ语句:

[csharp] view plain copy
  1. // query with lamda expression  
  2. var QueryWithLamda = empList.GroupBy(x => new { x.Age,  x.Sex})  
  3.             .Select(g=>new {g.Key, Count=g.Count()});   
  4.   
  5. //query with standard expression  
  6. var query=from el in empList  
  7.           group el by new {el.Age,el.Sex} into g  
  8.           select new {g.Key, Count=g.Count()};   

 

现在需要用VB.Net来实现,根据上边C#的写法,很容易想到这样来写:

  1. ' query with lamda  expression    
  2. Dim QueryWithLamda =  empList.GroupBy(Function(x)  New With { .Age=x.Age, .Sex= x.Sex}) _   
  3.                              .Select(Function(g) New With {g.Key, g.Count()})    
  4.   
  5.   
  6. ' query with standard expression  
  7. Dim Query = From el In empList _   
  8.             Group el By Key = new with { el.Age,  el.Sex} Into g= Group  _   
  9.             Select New  With {.key = Key, _  
  10.                             .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 

  1. Sub Main  
  2.      Dim empList As New List(Of Employee)()  
  3.         empList.Add(New Employee() With _  
  4.         {.ID = 1, .FName = "John", .Age = 23, .Sex = "M"c})   
  5.         empList.Add(New Employee() With _  
  6.         {.ID = 2, .FName = "Mary", .Age = 25, .Sex = "F"c})  
  7.         empList.Add(New Employee() With _  
  8.         {.ID = 3, .FName = "Amber", .Age = 23, .Sex = "M"c})   
  9.         empList.Add(New Employee() With _  
  10.         {.ID = 4, .FName = "Kathy", .Age = 25, .Sex = "F"c})  
  11.         empList.Add(New Employee() With _  
  12.         {.ID = 5, .FName = "Lena", .Age = 27, .Sex = "F"c})   
  13.         empList.Add(New Employee() With _  
  14.         {.ID = 6, .FName = "Bill", .Age = 28, .Sex = "M"c})  
  15.         empList.Add(New Employee() With _  
  16.         {.ID = 7, .FName = "Celina", .Age = 27, .Sex = "F"c})   
  17.         empList.Add(New Employee() With _  
  18.         {.ID = 8, .FName = "John", .Age = 28, .Sex = "M"c})  
  19.           
  20.     ' query with lamda  expression    
  21.     Dim QueryWithLamda = empList.GroupBy(Function(x)  New With { Key x.Age, Key x.Sex}) _  
  22.                                 .Select(Function(g) New With {g.Key, g.Count()})   
  23.       
  24.     ' query with standard expression  
  25.     Dim QueryWithStanard = From el In empList _   
  26.                 Group el By Key = new with {Key el.Age, Key el.Sex} Into g= Group  _   
  27.                 Select New  With {.key = Key, _  
  28.                                 .count = g.Count()}   
  29.                                   
  30.     Dim QueryWithStanard2 = From el In empList _   
  31.                 Group el By Key = el.Age,  el.Sex  Into g= Group  _   
  32.                 Select New  With {.key = Key, _  
  33.                                 .count = g.Count()}                               
  34.    
  35.     For Each employee In QueryWithLamda  'Or  QueryWithLamda  
  36.              Console.WriteLine(employee.Count)  
  37.     Next employee   
  38.   
  39. End Sub  
  40. Public Class Employee   
  41.         Private privateID As Integer  
  42.         Public Property ID() As Integer   
  43.   
  44.             Get   
  45.                 Return privateID  
  46.             End Get   
  47.   
  48.             Set(ByVal value As Integer)   
  49.                 privateID = value  
  50.             End Set   
  51.   
  52.         End Property   
  53.   
  54.         Private privateFName As String   
  55.         Public Property FName() As String  
  56.             Get   
  57.                 Return privateFName  
  58.             End Get   
  59.   
  60.             Set(ByVal value As String)   
  61.                 privateFName = value  
  62.             End Set   
  63.         End Property    
  64.   
  65.         Private privateAge As Integer   
  66.         Public Property Age() As Integer  
  67.             Get   
  68.                 Return privateAge  
  69.             End Get   
  70.   
  71.             Set(ByVal value As Integer)   
  72.                 privateAge = value  
  73.             End Set   
  74.         End Property    
  75.   
  76.         Private privateSex As Char   
  77.         Public Property Sex() As Char  
  78.             Get   
  79.                 Return privateSex  
  80.             End Get   
  81.   
  82.             Set(ByVal value As Char)   
  83.                 privateSex = value  
  84.             End Set   
  85.         End Property  
  86. 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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值