简体   繁体   中英

Expression Call with Type Combining lambda linq where

I am trying to create a dynamic lookup filter for a 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 .

I am trying to reproduce the example of 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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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