If such a list exists, you need to perform group by operation based on the age and sex columns:
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 # Is a good implementation. Generally, write the LINQ statement as follows:
// 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()};
Now we need to use VB. NET to implement it. According to the above C # statement, it is easy to think of writing like this:
' query with lamdaexpressionDim 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 expressionDim 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()}
However, the printed results showed that they were incorrect. After careful research, the original problem was as follows:
Group El by key = new with {El. Age, El. Sex} into G = group _
In this case, the result is the same as that of C:
Group El by key = new with {key El. Age, key El. Sex} into G = group _
It seems that in VB. NET, anonymous classes are different from C #. When group by has multiple columns, you must specify all of them as keys. Otherwise, they will not be grouped by as a whole.
All the code is as follows:
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 code:
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
Related discussion posts:
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