i want to create the following query in expression trees:
var test = from datarow in tempResults
where datarow.Field<String>("ColumnName") == "Column"
select datarow;
How do i create the expression:
datarow.Field<String>("ColumnName")?
i tried everything, i even got stuck on getting the MethodInfo of Field for the Expression.Call method.
Field is an extention Method of DataRowExtentions.
Do i have to use Expression.Call() for this?
How do i get the MethodInfo?
is there a simplier way to do it ?
i tried :
ParameterExpression dataRow = Expression.Parameter(typeof(DataRowExtensions), “dataRow”);
Expression left = Expression.Call(dataRow, typeof(DataRowExtensions).GetMethod(“Field”));
but it doesn’t work.
i want to create dynamic filters on the data inside IQueryable tempResults.
The user will check on checkboxes on the GUI that will add ‘Where’ expressions to the data in tempResults. when the user chooses “Column” i want to present the DataRows where ColumnName = “Column”.
that is why i need to create the where expression. but i’m so stuck on the MethodInfo thing. I tried this too:
MethodInfo FieldStringMethodInfo = typeof(DataRowExtensions).GetMethod("Field", BindingFlags.Public | BindingFlags.Static);
but it doesn’t work too.
Is there other ways to do it ?
Replacement answer following clarification in comments:
For successively building additional filters, you don’t need expression trees; you can call
.Wheremultiple times (as necessary, once per search term) – for example:The only thing to watch is the “capture” issue; be sure not to re-use any of the
value1,value2etc – otherwise the last value will apply to earlier filters…For an example of delegate combination (from comments) – note that I’ve dropped the
DataTableaspect here purely to make the example shorter (it will work identically):(original answer)
Actually, that variant of LINQ won’t use an expression tree… it will use a delegate; but you can build the tree and compile it if you really want… I’m not sure why you would, though. What do you want to do? I’ll knock up an example…
Here you go; this uses an expression tree, but I can’t think of a single good reason to do this, other than to prove that you can!