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

发布于 2024-12-03 22:48:19 字数 3457 浏览 0 评论 0原文

我试图模仿左外连接 此处 但使用动态 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)));
}

但是我失败的地方是 SelectMany 中的 DefaultIfEmpty

I am trying to mimick the left outer join here but using dynamic linq extension methods. What i have:

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)));            
}

and:

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)));
}

However where I fall down is with the DefaultIfEmpty within the SelectMany

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-12-10 22:48:20

添加 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)");

Add void DefaultIfEmpty(); to interface IEnumerableSignatures

Then use

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));
}

Then you have a call like

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)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文