Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the solution that I have come up with, my biggest help came from <a href="https://stackoverflow.com/questions/439172/calling-a-method-from-an-expression">Calling a Method from an Expression</a> The biggest thing to over come was to create a generic "Any" method. The rest was just chaining the calls together.</p> <pre><code>private MethodCallExpression BuiltMethodCall(IQueryable _query, string CustTypeID, Type _ParentObjType, Type _ChildObjType, string strChildObj, string strChildCol) { //This function will build a dynamic linq expression tree representing the ling calls of: //Customers.Where(c =&gt; c.CustomerDemographics.Any(cd =&gt; cd.CustomerTypeID = custTypeID)) ConstantExpression value = Expression.Constant(CustTypeID); //Build the outer part of the Where clause ParameterExpression parameterOuter = Expression.Parameter(_ParentObjType, "c"); MemberExpression propertyOuter = Expression.Property(parameterOuter, strChildObj); //Build the comparison inside of the Any clause ParameterExpression parameterInner = Expression.Parameter(_ChildObjType, "cd"); MemberExpression propertyInner = Expression.Property(parameterInner, strChildCol); BinaryExpression comparison = Expression.Equal(propertyInner, value); LambdaExpression lambdaInner = Expression.Lambda(comparison, parameterInner); //Create Generic Any Method Func&lt;MethodInfo, bool&gt; methodLambda = m =&gt; m.Name == "Any" &amp;&amp; m.GetParameters().Length == 2; MethodInfo method = typeof(Enumerable).GetMethods().Where(methodLambda).Single().MakeGenericMethod(_ChildObjType); //Create the Any Expression Tree and convert it to a Lambda MethodCallExpression callAny = Expression.Call(method, propertyOuter, lambdaInner); LambdaExpression lambdaAny = Expression.Lambda(callAny, parameterOuter); //Build the final Where Call MethodCallExpression whereCall = Expression.Call(typeof(Queryable), "Where", new Type[] { _query.ElementType }, new Expression[] { _query.Expression, Expression.Quote(lambdaAny) }); return whereCall; </code></pre> <p>}</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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