16,722
社区成员




- from s as clsStudent in lstStudent group s by new {s.Age, s.Gender, s.ClassID} into mygrp ...‘这样就通不过,后面不会写了
'发觉这两种方式都可以,发楼主参考:
Dim query1=From x in empList _
Group x By x.Age, x.Sex Into g=Group _
Select New With {g,g.Count()}
Dim query2=From el In empList _
Group el By Key = new with {key el.Age, key el.Sex} _
Into Group _
Select New With {.key = Key, _
.count = Group.Count()}
Dim query= From s in lstStudent _
Group by k= New With {s.Age} into g = Group _
Select New with _
{ .StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}
Dim query= From s in lstStudent _
Group s by s.Age,s.Gender, s.ClassID into g = Group _
Select New with _
{ .StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}
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})
Dim query = empList.GroupBy(Function(x) New With { .Age=x.Age, .Sex= x.Sex}) _
.Select(Function(g) New With {g.Key, g.Count()})
For Each employee In query
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
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'}
};
var query = empList.GroupBy(x => new { x.Age, x.Sex})
.Select(g=>new {g.Key, Count=g.Count()});
foreach (var employee in query )
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;}
}
dim lstStudent as new list(of clsStudent)
for i as integer =0 to 3
dim tempS as new clsStudent
with tempS
.Age=15
.Gender="M"
.ClassID=1
.StudentID=i
.Name="Test_" & i
end with
lstStudent.add (tempS)
next
dim lstStudent as new list(of clsStudent)
for i as integer =0 to 3
dim tempS as new clsStudent
with tempS
.Age=15
.Gender=M
.ClassID=1
.StudentID=i
.Name="Test_" & i
end with
lstStudent.add (tempS)
next
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with _
{
.Age=g.Key.Age , _
.Gender=g.Key.Gender, _
.ClassID=g.Key.ClassID, _
.StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with
{
.Age=g.Key.Age ,
.Gender=g.Key.Gender,
.ClassID=g.Key.ClassID,
.StudentID=string.Join(",",g.Select(x=>x.StudentID).ToArray()),'假定 StudentID为string 类型
.Name=string.Join(",",g.Select(x=>x.Name).ToArray()),'假定 Name为string 类型
.Count=g.Count()
}