left outer join with multiple conditions using Dynamic Linq

c# dynamic-linq linq


I am trying to build Dynamic Left outer join Linq query.

I took help of How do I do a left outer join with Dynamic Linq?

I am getting the following error in "SelectMany".

 System.Linq.Dynamic.ParseException: 'No property or field 'ls' exists in type 'DataStore''

Code I have is :

public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object[] values)
    if (inner == null) throw new ArgumentNullException("inner");
    if (outerSelector == null) throw new ArgumentNullException("outerSelector");
    if (innerSelector == null) throw new ArgumentNullException("innerSelector");
    if (resultsSelector == null) throw new ArgumentNullException("resultsSelctor");

    LambdaExpression outerSelectorLambda = DynamicExpression.ParseLambda(outer.ElementType, null, outerSelector, values);        
    Type enumType = typeof(IEnumerable<>).MakeGenericType(inner.AsQueryable().ElementType);
    LambdaExpression innerSelectorLambda = DynamicExpression.ParseLambda(inner.AsQueryable().ElementType, null, innerSelector, values);
    ParameterExpression[] parameters = new ParameterExpression[] {
        Expression.Parameter(outer.ElementType, "outer"), Expression.Parameter(enumType, "inner")};
    LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, resultsSelector, values);

    return outer.Provider.CreateQuery(
            typeof(Queryable), "GroupJoin",
            new Type[] { outer.ElementType, inner.AsQueryable().ElementType, outerSelectorLambda.Body.Type, resultsSelectorLambda.Body.Type },
                outer.Expression, inner.AsQueryable().Expression, Expression.Quote(outerSelectorLambda), Expression.Quote(innerSelectorLambda), Expression.Quote(resultsSelectorLambda)));

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");
    LambdaExpression lambda = DynamicExpression.ParseLambda(
        source.ElementType, null, selector, values);    
    Type inputType = source.Expression.Type.GetGenericArguments()[0];
    Type resultType = lambda.Body.Type.GetGenericArguments()[0];            
    Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
    Type delegateType = typeof(Func<,>).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.Expression, Expression.Quote(lambda),

public static object DefaultIfEmpty(this IQueryable source)
    if (source == null) throw new ArgumentNullException("source");
    return source.Provider.Execute(
            typeof(Queryable), "DefaultIfEmpty",
            new Type[] { source.ElementType },

Datastore is a class which supports table-column-like data store
Supports ONLY string-indexed accessors:

ds["Name"] = "John"
    new DataStore("Employee"){ ["Name"] = "John"} 

I am calling the GroupJoin like this :

 var qry = otherSource.AsQueryable().GroupJoin(source, "new(it["FirstName"] as key0,it["City"] as key1)", "new(it["FirstName"] as key0,it["City"] as key1)", "new(inner as ls , outer as rs)").SelectMany("ls.DefaultIfEmpty()", "new(inner.ls as l, rs as r)");

The GroupJoin executes fine but the problem is with the value for selector parameter of SelectMany. i.e.. "ls.DefaultIfEmpty()"

What should I be passing for selector parameter of SelectMany?

8/23/2018 2:21:02 AM

Popular Answer

Did you also try the newer (.NET Core) version which does support GroupJoin out of the box?

See this code.

9/4/2018 7:15:47 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow