如何使用 Dynamic Linq 进行左外连接?

Posted

技术标签:

【中文标题】如何使用 Dynamic Linq 进行左外连接?【英文标题】:How do I do a left outer join with Dynamic Linq? 【发布时间】:2011-11-13 22:23:51 【问题描述】:

我试图模仿左外连接here,但使用动态 linq 扩展方法。我有什么:

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<> type in case  
    // the expression parsed to something other than IEnumerable<T>. 
    // For instance, a expression evaluating to List<T> would result  
    // in a lambda of type Func<T, List<T>> when we need one of type 
    // an Func<T, IEnumerable<T> in order to call SelectMany(). 
    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.ElementType, 
            resultType, 
            resultsSelectorLambda.Body.Type 
        , source.Expression, Expression.Quote(lambda), 
        Expression.Quote(resultsSelectorLambda)));            

和:

public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner,
    string outerKeySelector, string innerKeySelector, string resultSelector, 
    params object[] values)

    Type innerElementType = inner.AsQueryable().ElementType;

    var outerParameter = Expression.Parameter(outer.ElementType, "outer");
    var innerParameter = Expression.Parameter(innerElementType, "inner");
    var groupParameter = Expression.Parameter(typeof(IEnumerable<>)
        .MakeGenericType(innerElementType), "group");

    var outerLambda = DynamicExpression.ParseLambda(new[]  outerParameter ,
        null, outerKeySelector, values);
    var innerLambda = DynamicExpression.ParseLambda(new[]  innerParameter ,
        outerLambda.Body.Type, innerKeySelector, values);
    var resultLambda = DynamicExpression.ParseLambda(new[]  
        outerParameter, groupParameter , null, resultSelector, values);

    return outer.Provider.CreateQuery(Expression.Call(typeof(Queryable), 
        "GroupJoin", new[]  outer.ElementType, innerElementType, 
        outerLambda.Body.Type, resultLambda.Body.Type ,
        outer.Expression, Expression.Constant(inner),
        Expression.Quote(outerLambda), Expression.Quote(innerLambda),
        Expression.Quote(resultLambda)));

但是我在DefaultIfEmpty 中的SelectMany 中失败了

【问题讨论】:

这里指的是 DynamicExpression 的哪个实现? 【参考方案1】:

void DefaultIfEmpty(); 添加到interface IEnumerableSignatures

然后使用

public static object DefaultIfEmpty(this IQueryable source)

    if (source == null) throw new ArgumentNullException("source");
        return source.Provider.Execute(
    Expression.Call(
        typeof(Queryable), "DefaultIfEmpty",
        new Type[]  source.ElementType ,
        source.Expression));

然后你有一个类似的电话

var qry = Foo.GroupJoin(Bar, "outer.Id", "inner.Id", "new(outer.Id as Foo, group as Bars)").SelectMany("Bars.DefaultIfEmpty()", "new(outer.Foo as Foo, inner as Bar)");

【讨论】:

你们能提供更简单的信息吗?我需要使用这个:(

以上是关于如何使用 Dynamic Linq 进行左外连接?的主要内容,如果未能解决你的问题,请参考以下文章

LINQ查询中的左外连接[重复]

如何使用 linq lambda 扩展方法执行带有 where 子句的左外连接

Linq to Sql:多个左外连接

Linq to SQL 使用 Lambda 语法进行左外连接并在 2 列上连接(复合连接键)

如何在 Linq 中执行左外连接? [复制]

如何将 LINQ 左外连接限制为一行