Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have added another SelectMany that retuns an AnonymousType. </p> <pre><code> public static IQueryable SelectMany(this IQueryable source, string selector, string resultsSelector, params object[] values) { if (source == null) throw new ArgumentNullException("source"); if (selector == null) throw new ArgumentNullException("selector"); // Parse the lambda LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values); // Fix lambda by recreating to be of correct Func&lt;&gt; type in case // the expression parsed to something other than IEnumerable&lt;T&gt;. // For instance, a expression evaluating to List&lt;T&gt; would result // in a lambda of type Func&lt;T, List&lt;T&gt;&gt; when we need one of type // an Func&lt;T, IEnumerable&lt;T&gt; in order to call SelectMany(). Type inputType = source.Expression.Type.GetGenericArguments()[0]; Type resultType = lambda.Body.Type.GetGenericArguments()[0]; Type enumerableType = typeof(IEnumerable&lt;&gt;).MakeGenericType(resultType); Type delegateType = typeof(Func&lt;,&gt;).MakeGenericType(inputType, enumerableType); lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters); ParameterExpression[] parameters = new ParameterExpression[] { Expression.Parameter(source.ElementType, "outer"), Expression.Parameter(resultType, "inner") }; LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, resultsSelector, values); // Create the new query return source.Provider.CreateQuery( Expression.Call( typeof(Queryable), "SelectMany", new Type[] { source.ElementType /*TSource*/, /*,TCollection*/resultType /*TResult*/, resultsSelectorLambda.Body.Type}, source.Expression, Expression.Quote(lambda), Expression.Quote(resultsSelectorLambda))); } </code></pre> <p>I still need to figure out how to do the following using Dynamic, the goal is to return a new result object.</p> <pre><code> var customerandorderflat = db.Customers .SelectMany(c =&gt; c.Orders.SelectMany(o =&gt; o.Order_Details, (ord, orddetail) =&gt; new { OrderID = ord.OrderID, UnitPrice = orddetail.UnitPrice }).DefaultIfEmpty(), (cus, ord) =&gt; new { CustomerId = cus.CustomerID, CompanyName = cus.CompanyName, OrderId = ord.OrderID == null ? -1 : ord.OrderID, UnitPrice = ord.UnitPrice }); </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