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
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.
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]);
}