Group By的VB.Net Linq InvalidCastException

时间:2010-12-12 08:34:13

标签: vb.net linq exception group-by linq-to-objects

我有一个可行的Linq Group By查询。这是查询:

        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

我不喜欢使用匿名类型,因此我试图取消结果并且遇到 InvalidCastException

以下是类型信息:

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]]

因此,有了这些信息,这里是使用的声明:

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

这是返回的错误:

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]]]'.

丑陋的解决方案是定义一个对象并逐步完成结果:

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

加载集合:

    Dim FeedbackData As New List(Of FeedbackDataItem)

    For Each item In query
        Dim fbi As New FeedbackDataItem(item.transaction_id)
        fbi.Feedbacks.AddRange(item.Group)
        FeedbackData.Add(fbi)
    Next

我很难理解为什么会收到这个错误。定义结果并重新运行它们会更好,而不是人工处理数据。我错过了什么吗?

2 个答案:

答案 0 :(得分: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 :(得分: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 = ...
            }