Question:
I'm using a modified version of Linq.Dynamic to sort a datatable upon an ajax request (see code below). So far it used to work fine.
However, I have a problem:
If I have a datatable, that contains a NULL value, even if in only one field in only one row is NULL, I get those two exceptions:
Object must be of type "string"
and if several adjacent values are NULL:
At least one object must implement IComparable
This is my code
using System.Linq;
using System.Data;
using System.Linq.Dynamic;
// Pre:
// sidx: Sort Field
// sord: Sort order ["ASC", "DESC"]
// page: current page number
// rows: pagesize in rows
public static string TestPaging(string sidx, string sord, string page, string rows)
{
string strReturnValue = null;
using (System.Data.DataTable dtAllData = GetDataTable())
{
IQueryable<System.Data.DataRow> iqOrderedPagedData = null;
if (string.IsNullOrEmpty(sidx) || string.IsNullOrEmpty(sord))
iqOrderedPagedData = dtAllData.AsEnumerable().AsQueryable();
else
iqOrderedPagedData = dtAllData.AsEnumerable().AsQueryable().OrderBy(sidx + " " + sord);
Int32 iPageSize = string.IsNullOrEmpty(rows) ? 100 : System.Convert.ToInt32(rows);
Int32 iPageIndex = System.Convert.ToInt32(page) - 1;
iqOrderedPagedData = iqOrderedPagedData.Skip(iPageIndex * iPageSize).Take(iPageSize);
//using (System.Data.DataTable dtOrderedPagedData = MyCopyToDataTable(iqOrderedPagedData))
using (System.Data.DataTable dtOrderedPagedData = iqOrderedPagedData.CopyToDataTable())
{
cjqGrid jqGrid = new cjqGrid();
//jqGrid.total = dtAllData.Rows.Count / iPageSize + 1;
jqGrid.total = (int)Math.Ceiling((float)dtAllData.Rows.Count / (float)iPageSize);
jqGrid.page = iPageIndex + 1;
jqGrid.records = dtAllData.Rows.Count;
jqGrid.data = dtOrderedPagedData;
strReturnValue = null; // Serialize(jqGrid, true);
jqGrid = null;
} // End Using dtOrderedPagedData
} // End Using dtAllData
//Response.ContentType = "application/json";
return strReturnValue;
}
TestPaging("USR_Domain", "desc", "1", "10");
The problem seems to be the extension method CopyToDataTable, at the line:
if (!e.MoveNext ())
This makes it sort the table, which means it calls the function Compare in class System.Linq.SortSequenceContext
where the error is thrown at this line
comparison = comparer.Compare(keys[first_index], keys[second_index]);
Here the version from mono with my fixes which make that mono method actually work.
However, the bug occurs in plain old MS .NET 4.0 as well.
(I need mono to backport my Linq-using method to .NET 2.0)
public static DataTable CopyToDataTable<T> (this IEnumerable<T> source)
where T : DataRow
{
DataTable dt = new DataTable ();
IEnumerator<T> e = source.GetEnumerator ();
if (!e.MoveNext ())
throw new InvalidOperationException ("The source contains no DataRows");
foreach (DataColumn col in e.Current.Table.Columns)
dt.Columns.Add (new DataColumn (col.ColumnName, col.DataType, col.Expression, col.ColumnMapping));
CopyToDataTable<T> (source, dt, LoadOption.PreserveChanges);
return dt;
}
public static void CopyToDataTable<T> (this IEnumerable<T> source, DataTable table, LoadOption options)
where T : DataRow
{
if (object.ReferenceEquals(typeof(T), typeof(System.Data.DataRow)))
{
foreach (System.Data.DataRow drRowToCopy in source)
{
System.Data.DataRow drNewRow = table.NewRow();
for (int i = 0; i < drRowToCopy.ItemArray.Length; ++i)
{
drNewRow[i] = drRowToCopy[i];
} // Next i
table.Rows.Add(drNewRow);
} // Next dr
}
else
CopyToDataTable<T>(source, table, options, null);
}
To problem occurs as soon as even only one value in one column of one row is NULL...
Can anyone suggest me how I can fix this problem ?
Or how I can otherwise make a DataTable from an IEnumerable without getting exceptions when one or two fields are NULL ?
Right now I "fixed" it by catching the exception in Compare.
But that's only a fix for .NET 2.0, where I can do this because this class doesn't exist.
I need a real fix that works for .NET 4.0 as well.
public override int Compare (int first_index, int second_index)
{
int comparison = 0;
try
{
comparison = comparer.Compare(keys[first_index], keys[second_index]);
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
}
if (comparison == 0) {
if (child_context != null)
return child_context.Compare (first_index, second_index);
comparison = direction == SortDirection.Descending
? second_index - first_index
: first_index - second_index;
}
return direction == SortDirection.Descending ? -comparison : comparison;
}
PS: For my DataTable-OrderBy-Enhanced version of Linq.Dynamic, see here:
http://pastebin.com/PuqtQhfa