OrderBy and List vs. IOrderedEnumerable

I ran into an unexpected problem with the following code.

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

This code generates an error:

It is not possible to implicitly convert the type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.List'. Explicit conversion exists (are you skipping listing?)

It seems I can change items to type IEnumerable<string> and the error will disappear. But I need to add items to a list that IEnumerable does not support.

Can someone help me understand this error and what is the easiest solution? Is it possible to simply give a result?

+19
c # linq
Feb 14 2018-12-12T00:
source share
6 answers

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

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

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

Solution suggested by others:

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

... creates a new list with original items in a new order. This is only useful if you need to keep the original order for some other purpose; it's rather more useless for memory than sorting a list in place.

As for understanding the error, it's simple: List<T> not a subtype of IOrderedEnumerable<T> , so there is no implicit conversion of links between them. The explicit offer that the compiler offers will satisfy the compiler, but it will not work at runtime because the object returned by OrderBy<T> does not inherit from List<T> .

EDIT

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

 List<MyType> items = GetSomeItems(); items.Sort((a, b) => a.Key.CompareTo(b.Key)); 
+33
Feb 14 2018-12-12T00:
source share

You need to convert IEnumerable to List . Try the following:

 items = items.OrderBy(item => item).ToList(); 
+14
Feb 14 2018-12-14T00:
source share

try it

 items = items.OrderBy(item => item).ToList(); 
+6
Feb 14 2018-12-12T00:
source share

You need to use the LINQ ToList () method

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

You cannot directly using IEnumerable <> to List <>

+5
Feb 14 2018-12-12T00:
source share

You don't need Linq in the first place to sort the list of strings - just use Sort() :

 List<string> items = new List<string>(); //add items here items.Sort(); 
+4
Feb 14 2018-12-12T00:
source share

OrderBy () is an IEnumerable extension method, not a List.

When the compiler encounters the OrderBy () extension method, it translates the range variable into IOrderedEnumerable, where it can perform the required sorting using the CreateOrderedEnumerable method using IComparer et al. After sorting, the compiler spills out the variable as IEnumerable - usually.

Suggestion: Use the var keyword to enter "elements" in the LinQ clause.

Of course, the options proposed above using the Sort () and ToList () methods will work, however, their use is associated with greedy operators, and you lose the advantage of lazy loading.

Here's a good breakdown: C # Sorting and comparing OrderBy between running Sort () and OrderBy ().

+1
Feb 11 '15 at 22:24
source share



All Articles