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 8083179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:22:43+00:00 2026-06-05T17:22:43+00:00

I need to return an a generic list in the correct order for my

  • 0

I need to return an a generic list in the correct order for my project, and I’m getting InvalidCastException errors. Here is the code:

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) x.Sequence). _
ThenBy(Function(x) x.SubSequence)

Note that:

  • CreateDate is a Nullable(Of DateTimeOffset)
  • Sequence is a Nullable(Of Int32)
  • SubSequence is a Nullable(Of Int32)

The exact error I’m getting is:

Unable to cast object of type
‘System.Linq.OrderedEnumerable2[DTDataUploader.Comment,System.Int32]'
to type 'System.Collections.Generic.List
1[DTDataUploader.Comment]’.

I’ve tried converting to the actual types…

Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
OrderBy(Function(x) x.CreateDate.Value). _
ThenBy(Function(x) Convert.ToInt32(x.Sequence)). _
ThenBy(Function(x) Convert.ToInt32(x.SubSequence))

… but I get the same error. What am I missing here?

  • 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-06-05T17:22:45+00:00Added an answer on June 5, 2026 at 5:22 pm

    LINQ operations like Where and OrderBy produce queries, not results. As the error states, the result of your full LINQ expression is an OrderedEnumerable(Of DTDataUploader.Comment, System.Int32), not a list.

    To turn this into a list, add a call to ToList() to the end of the expression.

    Dim lDt As List(Of Comment) = RemapCommentsForE1(so.CommentList). _
    OrderBy(Function(x) x.CreateDate.Value). _
    ThenBy(Function(x) x.Sequence). _
    ThenBy(Function(x) x.SubSequence).ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the code sample below. I need the ArrayList to be a generic List.
I need to return a list of counties, but I need to filter out
I need to return server time from a webservice. I've got the following code:
I have need to cast a generic list of a concrete type to a
I need to calculate the standard deviation of a generic list. I will try
I need to filter a generic List with data from a Json object. I
Given a generic List I would need some kind of index (in the database
I am converting DataTables to a generic list and need a quick and easy
Ok, so basically I need to create a generic method which takes a list
I'm trying to return a generic list after loading values from a file. However,

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.