Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3607448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:22:37+00:00 2026-05-18T21:22:37+00:00

I have a Linq Group By query that works. Here’s the query: Dim query

  • 0

I have a Linq Group By query that works. Here’s the query:

        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

I don’t like working with anonymous types so I’m trying to delare the result and am hitting an InvalidCastException.

Here’s the type information:

Debug.Print(query.GetType.ToString)

Returns:

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

and the inner type:

Debug.Print(item.GetType.ToString)

Returns:

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

So, armed with this information, here’s the declaration used:

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

Here’s the error returned:

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

The Ugly solution is to define an object and step through the results as follows:

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

And to load up the collection:

    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

I’m stumped as to the reason why I’m getting this error. It would be nicer to just define the results and retrun them rather that man-handle the data. Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:22:38+00:00Added an answer on May 18, 2026 at 9:22 pm

    The result of your query is an IEnumerable(Of anonymous type). If you don’t want it to be anonymous, you need to strongly type it.

    One way is to use the extension method syntax for the group by:

        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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have LINQ query such as: var authors = from x in
I have LINQ statement that looks like this: return ( from c in customers
I have a linq query and I am trying to put that in to
I have a basic LINQ Query that looks like the following: var results =
I have a linq query that is grouping by answers by QuestionGroup. I need
Is there a way to create LINQ query that will have different grouping fields.
I have a fairly complex LINQ query that joins several tables, and selects a
I have a linq query which presents data that is present in CustomersRecord Table
I have some Linq code and it's working fine. It's a query that has
I have a LINQ Query that creates a new type that contains a days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.