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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:44:47+00:00 2026-05-30T02:44:47+00:00

I ran across an unexpected problem with the following code. List<string> items = new

  • 0

I ran across an unexpected problem with the following code.

List<string> items = new List<string>();
items = items.OrderBy(item => item);

This code generates the error:

Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Collections.Generic.List’. An explicit conversion exists (are you missing a cast?)

It appears I can change items to be of type IEnumerable<string> and the error goes away. But I need to be able to add items to the list, which IEnumerable doesn’t support.

Can someone help me understand this error, and what the easiest fix is? Is it safe to simply cast the result?

  • 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-30T02:44:49+00:00Added an answer on May 30, 2026 at 2:44 am

    Why not just sort the list in place using the Sort() instance method; then you can add items to it later if you like:

    List<string> items = GetSomeItems();
    items.Sort();
    

    Or, use an ordered collection like a binary search tree. SortedSet<T> might fit the bill, depending on your needs.

    The solution suggested by the others:

    items = items.OrderBy(item => item).ToList(); 
    

    … creates another list with the original items in a new order. This is only useful if you need to preserve the original ordering for some other purpose; it’s rather more wasteful of memory than sorting the list in place.

    As far as understanding the error, it’s simple: List<T> isn’t a subtype of IOrderedEnumerable<T>, so there’s no implicit reference conversion between the two. The explicit cast that the compiler suggests will satisfy the compiler, but it will fail at run time because the object returned by OrderBy<T> does not inherit from List<T>.

    EDIT

    An example of List<T>.Sort(Comparison<T>), assuming the type MyType has a Key property of some type type T where T : IComparable<T>:

    List<MyType> items = GetSomeItems();
    items.Sort((a, b) => a.Key.CompareTo(b.Key));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do
I ran across the following code in Ely Greenfield's SuperImage from his Book component
I ran across this pattern in the code of a library I'm using. It
I ran across this code in one of our Entity Framework applications. I know
I ran across an interesting issue in some of my humanize_bytes() code. This loop
I ran across this segment of code (in .m implementation file in an IBAction
I just ran across the following syntax in a piece of Haskell code -
I ran across a new problem in the last week. Due to the nature
I ran across some code like this today; having a flow control flag that
I recently ran across some 3rd party C# code which does the following: public

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.