Inconsistent return types of linq queries with orderby clauses

Viewed 89

Does anyone know why the types of the result of these two queries is different?

// q1 is IOrderedEnumerable<int>
var q1 = from c1 in new[] { 1, 2, 3 }
         orderby c1
         select c1;

// q2 is IEnumerable<int>
var q2 = from c1 in new[] { 1, 2, 3 }
         from c2 in new[] { 1, 2, 3 }
         where c1 == c2
         orderby c1
         select c1;

I cannot work out why q2 is not also an IOrderedEnumerable<int>.

Using a 'join' clause doesn't make a difference:

// q3 is IEnumerable<int>
var q3 = from c1 in new[] { 1, 2, 3 }
         join c2 in new[] { 1, 2, 3 }
         on c1 equals c2
         orderby c1
         select c1;
2 Answers
Related