강력하게 입력 할 수 있도록 LINQ 그룹화 쿼리에 사용되는 개체를 정의하는 방법을 알 수 없습니다.복잡한 개체의 여러 키를 사용하여 LINQ 쿼리를 강력하게 입력하는 방법
두 개의 복잡한 개체를 키로 사용하는 그룹 쿼리를 작성했습니다. 쿼리가 작동하지만 반환 개체 형식을 선언 할 수 싶습니다. 나는 복잡한 유형이
... 나는 목록에서 수집하고이 복잡한 키를 기준으로 함께 그룹화하고있어
Public Class Student
Public Name As IndividualsName
Public EnrolledSchool As School
Public BirthHospital As BirthPlace
Public Grade As Integer
Public Sub New(ByVal name As IndividualsName, ByVal enrolledSchool As School, ByVal birthHospital As BirthPlace, ByVal grade As Integer)
Me.Name = name
Me.EnrolledSchool = enrolledSchool
Me.BirthHospital = birthHospital
Me.Grade = grade
End Sub
End Class
Public Class School
Inherits Location
Public Sub New(ByVal name As String, ByVal city As String, ByVal state As String)
MyBase.New(name, city, state)
End Sub
Public Shared Function GetMyHS() As School
Return New School("My High School", "My City", "My State")
End Function
Public Shared Function GetYourHS() As School
Return New School("Your High School", "Your City", "Your State")
End Function
Public Shared Function GetMyElementry() As School
Return New School("My Elementary", "My City", "My State")
End Function
Public Shared Function GetMyMiddleSchool() As School
Return New School("My Middle School", "My City", "My State")
End Function
End Class
Public Class IndividualsName
Public First As String
Public Last As String
Public Sub New(ByVal first As String, ByVal last As String)
Me.First = first
Me.Last = last
End Sub
Public Overrides Function ToString() As String
Return First & " " & Last
End Function
End Class
Public Class BirthPlace
Inherits Location
Public Sub New(ByVal name As String, ByVal city As String, ByVal state As String)
MyBase.New(name, city, state)
End Sub
Public Shared Function GetMyHospital() As BirthPlace
Return New BirthPlace("My General Hospital", "My City", "My State")
End Function
Public Shared Function GetYourHospital() As BirthPlace
Return New BirthPlace("Your General Hospital", "Your City", "Your State")
End Function
End Class
Public Class Location
Public Name As String
Public City As String
Public State As String
Public Sub New(ByVal name As String, ByVal city As String, ByVal state As String)
Me.Name = name
Me.City = city
Me.State = state
End Sub
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return Me.GetHashCode = obj.GetHashCode
End Function
Public Overrides Function GetHashCode() As Integer
Dim returnValue As Integer
returnValue = Me.Name.GetHashCode() Xor Me.City.GetHashCode() Xor Me.State.GetHashCode()
Return returnValue
End Function
End Class
.
Sub Main()
Dim students As List(Of Student) = New List(Of Student)
students.Add(New Student(New IndividualsName("Bill", "Jones"), School.GetMyHS(), BirthPlace.GetMyHospital(), 9))
students.Add(New Student(New IndividualsName("George", "Jamesen"), School.GetMyHS(), BirthPlace.GetMyHospital(), 11))
students.Add(New Student(New IndividualsName("Chris", "McCartney"), School.GetYourHS(), BirthPlace.GetMyHospital(), 9))
students.Add(New Student(New IndividualsName("Sara", "Smith"), School.GetMyMiddleSchool(), BirthPlace.GetMyHospital(), 7))
students.Add(New Student(New IndividualsName("Josh", "Jefferies"), School.GetMyMiddleSchool(), BirthPlace.GetYourHospital(), 8))
students.Add(New Student(New IndividualsName("Mel", "Tompson"), School.GetMyHS(), BirthPlace.GetMyHospital(), 12))
students.Add(New Student(New IndividualsName("Jill", "Schmidt"), School.GetYourHS(), BirthPlace.GetMyHospital(), 10))
students.Add(New Student(New IndividualsName("Beth", "Taylor"), School.GetMyElementry(), BirthPlace.GetMyHospital(), 5))
students.Add(New Student(New IndividualsName("Mark", "Thatcher"), School.GetMyElementry(), BirthPlace.GetMyHospital(), 4))
students.Add(New Student(New IndividualsName("Tom", "Jones"), School.GetMyHS(), BirthPlace.GetYourHospital(), 9))
students.Add(New Student(New IndividualsName("Kevin", "Woo"), School.GetMyMiddleSchool(), BirthPlace.GetYourHospital(), 7))
Dim groupedQuery 'As IEnumerable(Of IGrouping())
groupedQuery = From student In students _
Group student By student.EnrolledSchool, student.BirthHospital Into Group _
Select EnrolledSchool, BirthHospital, StudentGroup = Group
For Each groupItem In groupedQuery
Console.WriteLine(String.Format("School: {0}, Birth Place: {1}", groupItem.EnrolledSchool.Name, groupItem.BirthHospital.Name))
'Console.WriteLine(String.Format("Group Item is type {0}.", groupItem.GetType()))
For Each item As Student In groupItem.StudentGroup
Console.WriteLine(String.Format("Name: {0} - Grade: {1}", item.Name.ToString(), item.Grade.ToString()))
Next
Next
End Sub
모든 것이 작동하지만 데이터 수집을 처리 할 때 모든 것이 익명으로 선언됩니다. 특히 그룹화 된 목록을 만드는 개체가 하나의 어셈블리에 있고 웹 서비스 및 웹 사이트에서 사용되는 것이 매우 귀찮습니다.
정말 강력하게 반환되는 IEnumberable을 입력 할 수 있기를 원하지만 내 IGrouping interface을 선언하는 방법을 알 수 없습니다.
내가 좋아하는 내 데이터 구조를 횡단 할 수 있어야한다 ...
Dim groupedQuery As IEnumerable(Of IGrouping(Of IDoNotKnowHowToDeclareMyComplexKey, Student))
groupedQuery = From student In students _
Group student By student.EnrolledSchool, student.BirthHospital Into Group _
Select EnrolledSchool, BirthHospital, StudentGroup = Group
For Each groupItem As IGrouping(Of IDoNotKnowHowToDeclareMyComplexKey, Student) In groupedQuery
Console.WriteLine(String.Format("School: {0}, Birth Place: {1}", groupItem.Key.Name)
For Each item As Student In groupItem
Console.WriteLine(String.Format("Name: {0} - Grade: {1}", item.Name.ToString(), item.Grade.ToString()))
Next
Next
하지만 지금까지 내 IGrouping 유형을 선언하는 방법을 알아낼 수 없었다.
완벽. 그게 정확히 내가 찾고 있던 것이 었습니다. –