简体   繁体   English

类型组合为lambda linq的表达式调用,其中

[英]Expression Call with Type Combining lambda linq where

I am trying to create a dynamic lookup filter for a DataTable . 我正在尝试为DataTable创建动态查找过滤器。

The code look currently like this, I am looping through each Row/Column. 当前代码看起来像这样,我遍历每个行/列。 (one table feed the other one) (一张桌子喂另一张桌子)

    DataRow FoundRow=null;
             foreach (string ID in IDToCheck)
        {
                        FoundRow = IdTable.AsEnumerable().Where(row => row.Field<string>(ID).Equals(
                            RowInfo[ID].ToString(),StringComparison.InvariantCultureIgnoreCase)).First();
DoStuffWith(FoundRow);
        }

I do not manage to convert the row.Field<string>(ID) to Expression.Call . 我无法将row.Field<string>(ID)Expression.Call

I am trying to reproduce the example of Microsoft . 我正在尝试重现Microsoft的示例。

I don't know if you will really get any performance gain by that. 我不知道您是否真的会从中获得任何性能提升。

But, just to answer the direct question: "to convert the row.Field(ID) to Expression.Call" 但是,只是为了回答直接的问题:“将row.Field(ID)转换为Expression.Call”

IQueryable<DataRow> queryableData = IdTable.AsEnumerable().AsQueryable();

// Get the generice "Field<string>(string)" method from DataRowExtensions
ParameterExpression pe = Expression.Parameter(typeof(DataRow), "row");
MethodInfo fieldMethod = typeof (DataRowExtensions).GetMethod("Field", new [] {typeof(DataRow),typeof(string)});
MethodInfo genericFieldMethod = fieldMethod.MakeGenericMethod(typeof (string));

Expression left = Expression.Call(null, genericFieldMethod, pe, Expression.Constant( col_name ));
Expression right = Expression.Constant(value);
Expression exp = Expression.Equal(left, right);

IQueryable<DataRow> results = queryableData.Where(Expression.Lambda<Func<DataRow, bool>>(exp, pe));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM