2010-12-12 3 views
0

Linq Group By 쿼리가 작동합니다. 나는 결과를 delare려고하고 InvalidCastException이을 타격하고, 그래서 익명 형식 작업을 좋아하지 않는다VB.Net Linq 그룹에 대한 InvalidCastException

 Dim query = From fb As Feedback In lst Where fb.Seller.login_name.ToLower = UserName.ToLower 
        Order By fb.transaction_id Descending, fb.creation Descending _ 
        Group fb By fb.transaction_id _ 
        Into Group 

다음은 쿼리입니다.

Debug.Print(query.GetType.ToString) 

반환 :

System.Linq.GroupedEnumerable`4[Feedback,System.Nullable`1[System.Int32],Feedback,VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]] 

과 내부 유형 :

Debug.Print(item.GetType.ToString) 

결과 :

VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]] 
012,351,641 여기

는 유형 정보입니다

Unable to cast object of type 'System.Linq.GroupedEnumerable`4[Feedback,System.Nullable`1[System.Int32],Feedback,VB$AnonymousType_0`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]]' to type 'System.Collections.Generic.IEnumerable`1[System.Linq.IGrouping`2[System.Nullable`1[System.Int32],System.Collections.Generic.IEnumerable`1[Feedback]]]'. 

추한 용액은 다음과 같은 목적을 정의하고 결과를 탐색한다 : 여기서

Dim query As IEnumerable(Of IGrouping(Of Int32?, IEnumerable(Of Feedback))) 

에러가 반환 같습니다

그래서,이 정보로 무장 한, 여기에 사용 선언이다

0 :
Class FeedbackDataItem 

    Sub New(ByVal transaction_id As Integer) 
     _transaction_id = transaction_id 
     _feedbacks = New Feedbacks(Of Feedback) 
    End Sub 

    Private _transaction_id As Integer 
    Public Property transaction_id() As Integer 
     Get 
      Return _transaction_id 
     End Get 
     Set(ByVal value As Integer) 
      _transaction_id = value 
     End Set 
    End Property 

    Private _feedbacks As Feedbacks(Of Feedback) 
    Public Property Feedbacks() As Feedbacks(Of Feedback) 
     Get 
      Return _feedbacks 
     End Get 
     Set(ByVal value As Feedbacks(Of Feedback)) 
      _feedbacks = value 
     End Set 
    End Property 

End Class 

그리고 컬렉션을로드

이 오류가 발생하는 이유는 무엇입니까? 결과를 정의하고 데이터를 사람이 처리하는 대신 다시 테스트하는 것이 더 좋을 것입니다. 내가 놓친 게 있니?

답변

2

쿼리 결과는 익명 형식의 IEnumerable입니다. 익명으로하지 않으려면 강력하게 입력해야합니다.

한 가지 방법으로 그룹에 대한 확장 메서드 구문을 사용하는 것입니다

Dim feedbacks As IEnumerable(Of Feedback) = 
     From fb As Feedback In lst Where fb.Seller.login_name.ToLower = username.ToLower 
     Order By fb.transaction_id Descending, fb.creation Descending 

    Dim grouped As IEnumerable(Of IGrouping(Of Integer?, Feedback)) = 
     feedbacks.GroupBy(Function(fb) fb.transaction_id) 
+1

정확히 그게 내가 도달했습니다 :) 내 대답을 지금 삭제 ... –

+0

이것은 대단한 작품입니다 - 고마워요! – Graeme

1

문장의 끝 부분에 선택하여 원하는 유형으로 가져옵니다.

Dim query = From fb As Feedback In lst _ 
      Where fb.Seller.login_name.ToLower = UserName.ToLower 
      Order By fb.transaction_id Descending, fb.creation Descending _ 
      Group gr By fb.transaction_id 
      Into Group 
      Select new FeedbackDataItem with { 
       .transaction_id = gr.transaction_id, _ 
       .FeedBacks = ... 
      } 
+0

YER가 ... 같은 생각하지만 .transaction_id = fb.transaction_id이 잘못 – Graeme

+0

내 편집을 참조하십시오 - 선으로 그룹을 변경 –

관련 문제