C# 动态Linq到Datatable可空字段
我使用c#Dynamic Linq库编写针对数据表的自定义查询。我遇到的问题是,当我试图对具有空值的字段执行摘要操作时,会出现错误 我正在尝试执行类似以下内容的查询:C# 动态Linq到Datatable可空字段,c#,linq,dynamic,datatable,dbnull,C#,Linq,Dynamic,Datatable,Dbnull,我使用c#Dynamic Linq库编写针对数据表的自定义查询。我遇到的问题是,当我试图对具有空值的字段执行摘要操作时,会出现错误 我正在尝试执行类似以下内容的查询: var query = myDataTable.AsEnumerable().AsQueryable(); var newquery = query.GroupBy("new (get_item(@0).ToString() AS Forename)", "it", groupList.ToArray()); newqu
var query = myDataTable.AsEnumerable().AsQueryable();
var newquery = query.GroupBy("new (get_item(@0).ToString() AS Forename)", "it", groupList.ToArray());
newquery = newquery.Select("new (it.Key.Tier.ToString() as Tier, @0(it) as SumTotal", funcs.ToArray());
如果我要求和的列中有空值,则会出现错误“无法将DBNull.Value转换为类型“System.Double”。请使用可为空的类型。”
funcs数组包含用于执行Sum函数的lambda表达式。它是通过调用下面的函数构建的
public LambdaExpression GetGroupByLambdaExpression(Type groupByKeyType, string columnName, Type columnType, string expType)
{
ConstantExpression colParam = Expression.Constant(columnName, typeof(string));
MethodInfo fieldMethod = typeof(DataRowExtensions).GetMethod("Field", new Type[] {typeof(DataRow), typeof(string)});
fieldMethod = fieldMethod.MakeGenericMethod(columnType);
ParameterExpression rowParam = Expression.Parameter(typeof(DataRow), "r");
MethodCallExpression fieldMethodCall = Expression.Call(fieldMethod, rowParam, colParam);
dynamic columnExpression = Expression.Lambda(fieldMethodCall, rowParam);
MethodInfo sumMethod = null;
if (expType == "Count")
{
//Count will return 2 methods, we only want the first one with 1 parameter
sumMethod = typeof(Enumerable).GetMethods().Single(m => m.Name == expType & m.ReturnType.Equals(columnType) & m.IsGenericMethod & m.GetParameters().Count() == 1);
}
else if (expType == "Average")
{
//Average has multiple overrides so just use the first one
if (columnType == typeof(Int16) || columnType == typeof(Int32) || columnType == typeof(Int64))
{
sumMethod = typeof(Enumerable).GetMethods().First(m => m.Name == expType & m.ReturnType.Equals(typeof(double)) & m.IsGenericMethod);
}
else
{
sumMethod = typeof(Enumerable).GetMethods().First(m => m.Name == expType & m.ReturnType.Equals(columnType) & m.IsGenericMethod);
}
}
else
{
sumMethod = typeof(Enumerable).GetMethods().Single(m => m.Name == expType & m.ReturnType.Equals(columnType) & m.IsGenericMethod);
}
sumMethod = sumMethod.MakeGenericMethod(typeof(DataRow));
ParameterExpression groupParam = Expression.Parameter(groupByKeyType, "g");
MethodCallExpression sumMethodCall = null;
if (expType == "Count")
{
sumMethodCall = Expression.Call(sumMethod, groupParam);
}
else
{
sumMethodCall = Expression.Call(sumMethod, groupParam, columnExpression);
}
dynamic sumALambda = Expression.Lambda(sumMethodCall, groupParam);
return sumALambda;
}
有人知道如何处理datatable上的DbNull值吗?我完全被难住了有什么原因不能在开始查询数据表之前过滤它吗
var query = myDataTable.Where(val => !Convert.IsDbNull(val)).AsEnumerable().AsQueryable();
有什么原因不能在开始查询数据表之前对其进行筛选吗
var query = myDataTable.Where(val => !Convert.IsDbNull(val)).AsEnumerable().AsQueryable();
实际上,答案是在构建lambda表达式时用可为空的等价物替换DataTable列数据类型。实际上,答案是在构建lambda表达式时用可为空的等价物替换DataTable列数据类型。在我的实际应用程序中,我正在这样做多个摘要在每个摘要字段上过滤null将删除我不想删除的数据。在我的实际应用程序中,我正在做多个摘要,因此在每个摘要字段上过滤null将删除我不想删除的数据。