动态Linq到可数据化可空字段

时间:2012-08-24 13:25:58

标签: 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());

 newquery = newquery.Select("new (it.Key.Tier.ToString() as Tier, @0(it) as SumTotal", funcs.ToArray());

如果我总结的列有Null值,那么我得到一个错误“无法将DBNull.Value强制转换为'System.Double'。请使用可空类型。”

funcs数组包含一个lambda表达式来执行Sum函数。它是通过调用以下函数构建的。

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;
    }

有任何想法如何处理数据表上的DbNull值?我完全被难倒了

2 个答案:

答案 0 :(得分:0)

在开始查询数据表之前,是否有任何理由不能过滤数据表?

var query = myDataTable.Where(val => !Convert.IsDbNull(val)).AsEnumerable().AsQueryable();

答案 1 :(得分:0)

实际上,答案是在构建lambda表达式时将DataTable列数据类型替换为可以为空的等价物。