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 Query
- CopyToDataTable: Why exactly does it throw an error if one field is NULL, and how to fix it?
- I get a an error when the Date is null but cannot understand why and how to fix it
- What does this C# code with an "arrow" mean and how is it called?
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- How is ParallelEnumerable.Zip() inteded to work, and why does the behavior change when calling AsEnumerable()?
- Why does Resharper suggest a code change and then complain about the change?
- Why does my Linq query throw an exception when I try and sort on DateTime?
- Why am I getting a compile error on the ToList() and how do you separate each sql column into a sparate list element?
- How to select one column from datagrid and add data for the selected column from field text?
- Why does this Linq method throw a Null Reference Exception
- How to write SQL translateable linq code that groups by one property and returns distinct list
- How to pass or copy table field value from one table to another table using ASP.NET MVC and SQL Server
- Why does this LINQ statement return null and not a IEnumerable with count=0
- Using LINQ how would I check if a XML row contains a field and if it does check a different field on the same row for a value
- Find() and First() throws exceptions, how to return null instead?
- What are Expression Trees, how do you use them, and why would you use them?
- Why does Enumerable.Single() iterate all elements, even when more than one item has already been found?
- How to ask "Is there exactly one element satisfying condition" in LINQ?
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Why does Single() not return directly when more than one element is found?
- 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?
- How do you left join in Linq if there is more than one field in the join?
- How to return null if using SingleOrDefault() and searching a list of numbers for a number not in list?
- Why does this combination of Select, Where and GroupBy cause an exception?
- Why does this error occur when using SingleAsync?
- Why does Linq not have Head and Tail?
- Why does using anonymous type work and using an explicit type not in a GroupBy?
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- How to combine multiple fields into one field using LINQ
More Query from same tag
- Entity Framework grouped query translated into multiple SELECTs
- Filter on nested object in LINQ
- Group and concatenate List of tuples
- Is it possible to simplify the following Linq expression?
- Linq to Sql - Set connection string dynamically based on environment variable
- C# Linq search inside object linked property
- Complex LINQ to XML query assistance
- Linq query - conditional dependent columns from one table in .Net MVC Core 3.1, Entity Framework
- OrderBy list by a nested list
- Access to LINQ results - what is a better way?
- Nested Func<T, object> in a Generic Extension Method
- At least one one object must implement Icomparable
- How to Join with a column having Comma Separated Values in Linq
- how to use "not equal to" in where condition using linq with entity framework
- Combining two generics of type Dictionary<string, List<Model>> into another of the same type
- linq group by hour range?
- Avoid NVARCHAR when using ToList() in Linq
- Linq expression to select rows when no other row in the table matches a condition
- Code First EF 6 Table Per Hierarchy SQL query generation issue
- Select database entity depending of one of its datetime columns value
- Which use of Linq Any() is more efficient?
- LINQ and joining on selects
- Linq to XML select distinct value from
- Split IEnumerable in three parts: "above", "item", "below" with efficiency
- Linq if/else condition?
- Why an error occurs after I modified my model class?
- Splitting LINQ query based on predicate
- Making a list distinct in C#
- Recompile Query-Hint on a System.Data.Linq.DataContext object
- C# LINQ Oracle date Functions