Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling the .Any Extension Method with Entity Framework using Expression Trees
    text
    copied!<p>I have looked at few examples here <a href="https://stackoverflow.com/questions/439172/calling-a-method-from-an-expression">Calling a Method from an Expression</a> and on <a href="http://msdn.microsoft.com/en-us/library/bb882637.aspx" rel="nofollow noreferrer">MSDN</a> but I have not been able to get the right method call/object type for Any() for the query below. I seem to be able to get the property call but not IEnumerable part of the child property.<br> billing_map_set_lu is the parent of billmaps_lu and is defined as an association in the Entity Framework. </p> <p>The reason I am using expression trees is that I need to be able to define the query at runtime with 1-n .SelectMany(p => p.billmaps_lu).Where(predicate) clauses. So I figured if I could build the expression trees I could handle all the different combinations I have for this system which are many. </p> <pre><code>var myResults = ctx.billing_map_set_lu .Where(p =&gt; p.billmaps_lu.Any(b =&gt; b.billmap_columnname == "templatesittings_key" &amp;&amp; b.billmap_columnvalue == 428264)) SelectMany(p =&gt; p.billmaps_lu) .Where (b =&gt;b.billmap_columnname =="locations_key" &amp;&amp; b.billmap_columnvalue == 12445) Select(z =&gt; z.billing_map_set_lu); </code></pre> <p>I have tried a quite a few attempts using the samples above... </p> <pre><code>ParameterExpression bms = Expression.Parameter(typeof(billmaps_lu)); Expression left1 = Expression.Property(bms, typeof(billmaps_lu).GetProperty("billmap_columnname")); Expression right1 = Expression.Constant("templatesittings_key", typeof(string)); Expression InsideAny1 = Expression.Equal(left1, right1); Expression left2 = Expression.Property(bms, typeof(billmaps_lu).GetProperty("billmap_columnvalue")); Expression right2 = Expression.Constant(428264, typeof(int)); Expression InsideAny2 = Expression.Equal(left2, right2); Expression myWhereClause1 = Expression.AndAlso(InsideAny1, InsideAny2); </code></pre> <p>The above part seems fine but when I try to do the .Any It is like I can't get the right property/method to get the right objects out. (I feel like I am on a physics problem where I am working with the wrong units.) I am hoping it is something simple that I am missing, I am pretty new to Expression Trees.. I have included non-working code to try to show you where my head is at and how someone can steer me in the right direction. </p> <pre><code>MethodInfo method = typeof(Enumerable).GetMethods().Where(m =&gt; m.Name == "Any" &amp;&amp; m.GetParameters().Length == 2).Single().MakeGenericMethod(typeof(billing_map_set_lu).GetProperty("billmaps_lu").PropertyType); ParameterExpression billMapSetParameter = Expression.Parameter(typeof(billing_map_set_lu), "p"); ParameterExpression billMaps = Expression.Parameter(typeof(billmaps_lu), "p1"); var myFunction = Expression.Lambda&lt;Func&lt;billmaps_lu, bool&gt;&gt;(Expression.Call(method, Expression.Property(billMapSetParameter, typeof(billing_map_set_lu).GetProperty("billmaps_lu")), myWhereClause1), billMaps) </code></pre>
 

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