I believe you can use the approach from my answer to Execution-Deferred IQueryable<T> from Dynamic Linq?
First, add the same helper method from the link
public static class DynamicQueryableEx
{
public static IQueryable<TResult> Select<TResult>(this IQueryable source, string selector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
var dynamicLambda = System.Linq.Dynamic.DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
var memberInit = dynamicLambda.Body as MemberInitExpression;
if (memberInit == null) throw new NotSupportedException();
var resultType = typeof(TResult);
var bindings = memberInit.Bindings.Cast<MemberAssignment>()
.Select(mb => Expression.Bind(
(MemberInfo)resultType.GetProperty(mb.Member.Name) ?? resultType.GetField(mb.Member.Name),
mb.Expression));
var body = Expression.MemberInit(Expression.New(resultType), bindings);
var lambda = Expression.Lambda(body, dynamicLambda.Parameters);
return source.Provider.CreateQuery<TResult>(
Expression.Call(
typeof(Queryable), "Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression, Expression.Quote(lambda)));
}
}
Then you can use something like this
var result = listleftJn.Select<ResultOfSOurceAndDestination>(
"new (sources.SourceId as SourceId, destinations.DestinationID as DestinationID, sources.SecurityId as SecurityId, etc...)");
Don't forget to use sources
and destinations
accessors (that you defined inside the SelectMany
- "new(outer as sources, inner as destinations)"
). I would use in both places source
and destination
, or shorter s
and d
, but that's not essential as soon as they match.
UPDATE: There is also a problem in your SelectMany
projection.
"new(outer as sources, inner as destinations)"
should be
"new(outer.sources as sources, inner as destinations)"