Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic recursive lambda expressions
    primarykey
    data
    text
    <p>I want to create dynamic lambda expressions so that I can filter a list using a set of filtering parameters. This is what I have so far:</p> <p>The expression is built using the methods bellow, where T is the object type of the list</p> <pre><code> public static Expression&lt;Func&lt;T, bool&gt;&gt; GetExpression&lt;T&gt;(IList&lt;DynamicFilter&gt; filters) { if (filters.Count == 0) return null; ParameterExpression param = Expression.Parameter(typeof(T), "t"); Expression exp = null; if (filters.Count == 1) exp = GetExpression&lt;T&gt;(param, filters[0]); [...] return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(exp, param); } private static Expression GetExpression&lt;T&gt;(ParameterExpression param, DynamicFilter filter) { MemberExpression member = Expression.Property(param, filter.PropertyName); ConstantExpression constant = Expression.Constant(filter.Value); [...] return Expression.Call(member, filterMethod, constant); } </code></pre> <p>I then call</p> <pre><code>List&lt;Example&gt; list = ...; var deleg = ExpressionBuilder.GetExpression&lt;Example&gt;(dynFiltersList).Compile(); list = list.Where(deleg).ToList(); </code></pre> <p>This works just as expected with an object that contains only simple types, but if there are complex types inside, the code doesn't work anymore. For example, let's say I have a member of custom type Field inside the Example class and Field has a string property Value. If <code>filter.PropertyName</code> would be 'Field0' (of type Field), the code would work just fine, but if I have 'Field0.Value' I would get an obvious error stating that there is no property named 'Field0.Value' inside class Example.</p> <p>I tried modifying the expression building method, like this:</p> <pre><code> MemberExpression member = null; if (filter.PropertyName.Contains('.')) { string[] props = filter.PropertyName.Split('.'); ParameterExpression param1 = Expression.Parameter(typeof(T).GetProperty(props[0]).PropertyType, "t1"); member = Expression.Property(param1, props[0]); } else { member = Expression.Property(param, filter.PropertyName); } </code></pre> <p>but then I got a <code>Lambda parameter not in scope</code> error when compiling the expression. I sort of understand why I get this error, but I don't know how to make this work.</p> <p>Bottom line is I need to make the expression building method work recursively when forming the MemberExpression. I ultimately need to obtain a <code>list = list.Where(deleg).ToList();</code> that translates to something like this <code>list = list.Where(obj =&gt; obj.Field0.Value == 'something').ToList();</code></p> <p>I've just started working with expressions so I don't really know too much in this area, but any help would be appreciated.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload