I’m trying to sort a collection of type Enumerable<DataRow> by using LINQ’s OrderBy method on a nullable Int32 field. Because some of the values of this field are null, Visual studio throws a System.ArgumentException with message ‘Object must be of type Int32.’ Here is the problematic line of code:
collection1 = collection1.OrderBy(row => row["Column1"]);
where Column1 is the nullable Int32 field and the variable collection1 is declared as:
IEnumerable<DataRow> collection1;
Is there a way to rewrite the above line so that it ignores the nulls?
Try this: