LINQ Group by 多列值在C#与VB.Net上写法的区别
来源:互联网 发布:长沙seo网站排名优化 编辑:程序博客网 时间:2024/05/17 22:49
现有这样一个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 lamdaexpressionvar QueryWithLamda = empList.GroupBy(x => new { x.Age, x.Sex}) .Select(g=>new {g.Key, Count=g.Count()}); //query with standard expressionvar 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 lamdaexpressionDim 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 expressionDim 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 SubPublic Class Employee Private privateID As IntegerPublic Property ID() As Integer Get Return privateIDEnd Get Set(ByVal value As Integer) privateID = valueEnd Set End Property Private privateFName As String Public Property FName() As StringGet Return privateFNameEnd Get Set(ByVal value As String) privateFName = valueEnd Set End Property Private privateAge As Integer Public Property Age() As IntegerGet Return privateAgeEnd Get Set(ByVal value As Integer) privateAge = valueEnd Set End Property Private privateSex As Char Public Property Sex() As CharGet Return privateSexEnd Get Set(ByVal value As Char) privateSex = valueEnd Set End PropertyEnd 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://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
- LINQ Group by 多列值在C#与VB.Net上写法的区别
- LINQ Group by 多列值在C#与VB.Net上写法的区别
- linq group by Having 语句的写法.
- linq to DataTable group by用法(VB.Net语法)
- C#与vb.net的区别
- VB.NET与C#的一些区别
- C#与vb.net的区别
- C#与VB.NET的区别
- vb.net与c#的语法区别
- C#与VB.Net之间的区别
- vb.net与c#的语法区别
- order by 与group by的区别!
- group by 与 partition by的区别
- 在Asp.net中,(Entityframework,Linq的写法中) .ToList().Take(10)与.Take(10).ToList() 的区别
- Linq group by 的妙用
- linq group by 多列值 实现
- linq group by 多列值 实现
- mysql distinct 与 group by 的区别
- snd_pcm_new_internal
- def PrintTreeLikeTree(self):序
- ParseInorderNoRecursionNorStack(self):
- 树的遍历与打印
- 树的遍历与打印二
- LINQ Group by 多列值在C#与VB.Net上写法的区别
- android设置activity全屏 | 无标题
- IS08583报文协议包的解析和封装java源代码
- 学习数据库事半功倍有诀窍
- Dwz手册的补充说明和常见问题
- IS08583报文协议包的解析和封装java源代码
- 【原创】四点解密苹果 6月 最新搜索算法
- LINQ TO SQL Null 查询
- 基于StrutsTypeConverter的数组属性的类型转换器