OrderBy(x => x) failing

Description

When I try to use OrderBy(x => x) it fails with a System.NullReferenceException : Object reference not set to an instance of an object. The problem can be reproduced with the following test, which I ran inside NHibernate.Linq.Tests.OrderByTests.cs:

[Test]
public void SortSelfOrderByClause() {
//var orderedCities = nwnd.Customers.OrderBy(x => x.City).Select(x => x.City).Distinct(); // this line works
//var orderedCities = nwnd.Customers.Select(x => x.City).Distinct().OrderBy(x => x); // this line fails

var cities = nwnd.Customers.Select(x => x.City).Distinct();
var orderedCities = cities.OrderBy(x => x);

var cityList = cities.ToList();
var orderedCityList = orderedCities.ToList(); // choke here

Assert.AreEqual(cityList.Count, orderedCityList.Count);
Assert.Less(orderedCityList[0], orderedCityList[1]);
}

Environment

None

Activity

Show:

Patrick Schaller 
September 30, 2010 at 8:03 AM

I think this is not a bug.

A SQL-Statement always needs a specified column for.

SELECT * FROM customer c ORDER BY c.City

In your statement OrderBy(x => x) the Linq provider cannot determine a column for the SQL-statement.

When OrderBy(x => x) is evaluated in memory, the class should implement IComparable<> or you specify a special IComparer<>, otherwise orderby has not the expected effect.

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created August 28, 2009 at 8:09 AM
Updated September 30, 2010 at 8:03 AM
Who's Looking?