score:1
OK, i got the solution:
It was very obvious that it had something todo with the type System.DbNull
First, because I had only strings in my DataTable, and I got the error message:
Object must be of type "string"
But how can a string not be of type string ? Of course only if it's DbNull.
And if you got two adjacent DbNull.Values, you of course get:
At least one object must implement IComparable
Because DbNull doesn't implement IComparable.
After all, the funny thing was, that it did only fail when the sort column was a column with NULL values, but it did work perfectly if it was a column with no NULL values.
Since the table itselfs contains all the null values irrespective of the ordering, it was illogical that CopyToDataTable would sometimes not work, because it copied all values every time irrespective of the ordering.
The only logical conclusion was that OrderBy does NOT get executed when it is called in code, but only when some method was actually using the data produced by OrderBy.
A quick google search brought me to this http://blogs.msdn.com/b/charlie/archive/2007/12/09/deferred-execution.aspx
Of which only the first few lines needed to be read to know what the problem was:
This post covers one of the most important and frequently misunderstood LINQ features. Understanding deferred execution is a rite of passage that LINQ developers must undergo before they can hope to harness the full power of this technology.
So it dawned to me that I just passed the right of passage :)
Obviously the flaw is that e.MoveNext()
on ordering triggers a comparison which fails on DbNull, because DbNull as said does not implement IComparable.
Ironically, a datatable can also be sorted using the select statement, which I didn't know initially (after all, I'd expect a "order" method to be called "order", and not "select"...) So I just changed the OrderBy in Linq.Dynamic to
public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (ordering == null) throw new ArgumentNullException("ordering");
if (object.ReferenceEquals(source.ElementType, typeof(System.Data.DataRow)))
{
using (DataTable dt = source.Cast<System.Data.DataRow>().CopyToDataTable())
{
return dt.Select("", ordering).AsQueryable();
}
And voila, bug gone.
And since it can be filtered with Select alone more efficiently than when using Linq.Dynamic (which makes about 3 copies of all the data), I decided to abandon Linq.Dynamic completely.
I'm still using Linq take and skip, but in the future, I'll certainly be more reluctant to use Linq at all.
Deferred execution is outright dangerous, because it leads to very badly tracable bugs.
All that's needed for "boooom" is a null value at the wrong place, or a missing interface (and a missing check thereof or a missing generics restriction as in this case)...
Source: stackoverflow.com
Related Articles
- CopyToDataTable: Why exactly does it throw an error if one field is NULL, and how to fix it?
- ProtoBuf-Linq error message “ Invalid field in source data: 0”
- Why does this Linq method throw a Null Reference Exception
- The data source does not support server-side data paging
- What does this C# code with an "arrow" mean and how is it called?
- Sequence contains no elements error but I want to check for null
- C# 6 null conditional operator does not work for LINQ query
- Why does LINQ query throw an exception when I attempt to get a count of a type
- Why does not null in LINQ query still return null records?
- Why does this error occur when using SingleAsync?
- What does the error "Extension method must be static" mean?
- Does this LINQ code perform multiple lookups on the original data?
- LINQ SQL query check if a object field is not null
- LINQ to Entities Join on Nullable Field where Null Implies "Match All"
- ERROR Static method requires null instance, non-static method requires non-null instance
- Does this code really cause an "access to modified closure" problem?
- Nullable Field and SQL Is Null Issue
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Linq to sql error with identitiy increment field
- How to use Contains() function if field value is null in linq
- How does this linq code that splits a sequence work?
- SELECT NEW with a potentially null field using LINQ and Entity Framework
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- Getting null error LINQ; cannot then use?
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF Code First comparing null values generates strange query
- .First does not throw exception on empty collection
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- System.Linq.IOrderedQueryable' does not contain a definition for 'Skip' error while trying to use Skip method
- Query or algorithm to generate a structure to populate a tree (C#, LINQ)
- Multiple References of an entities in Entity Framework
- Linq ToString() how do I convert?
- Getting distinct objects from a list
- how to convert ListItemCollection (dropdownlist.items) to a dictionary<string,string>?
- How to a compose a Linq Expression to call OrderBy on a set of entities?
- Checking if no elements in IEnumerable(Of T) - Linq element and quantifier operators
- How can I make this SQL query work in LINQ lambda expression
- using Enumerable.Aggregate on System.Collection.Generic.Dictionary
- C# Entity Framework .ToList() but CA1002: Use Collection
- In Linq search a list using items in another list
- WPF Linq Binding join-qry to DataGrid
- How to get element value by using attributes in Linq to xml?
- Using reflection to address a Linqed property
- Count the specific feature in gridview using linq
- How to optimize linq query for speed?
- Executing stored procedure using linq in c#
- Linq: select grouped lists
- How to use Linq as an IF - Condition
- Asp MVC Entity Framework Check if Permission Is In Role