Yoyo
Yoyo

Reputation: 101

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.

Upvotes: 0

Views: 1077

Answers (1)

Ofir Winegarten
Ofir Winegarten

Reputation: 9355

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

Upvotes: 1

Related Questions