如何将动态GroupJoin结果转换为IQUERYABLE以执行Union或Concat操作?

时间:2016-01-19 13:20:53

标签: linq

我们将动态GroupJoin查询的结果作为IQueryable获取,我们必须在IQueryable上执行Union或concat。 下面是调用动态groupjoin函数的代码,它返回结果为IQueryable,我们使用链接How do I do a left outer join with Dynamic Linq?来获取GroupJoin返回的IQueryable结果

IQueryable leftOuterJoin= destination.AsQueryable().GroupJoin(source.AsQueryable(), "new(outer.SecurityID as SecurityID,outer.CUSIP as CUSIP)", "new(inner.SecurityID as SecurityID,inner.CUSIP as CUSIP)", "new (outer as source, group as destination )");

var rightOuterJoin= source.AsQueryable().GroupJoin(destination, "new(outer.SecurityID as SecurityID,outer.CUSIP as CUSIP)", "new(inner.SecurityID as SecurityID,inner.CUSIP as CUSIP)", "new (outer as source, group as destination )");

我们需要执行以下内容

var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);

帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

动态Linq已经为Union定义了IQueryable扩展方法,看起来像这样

public static IQueryable Union(this IQueryable source, IQueryable other)
{
    if (source == null) throw new ArgumentNullException("source");
    return source.Provider.CreateQuery(
        Expression.Call(
            typeof(Queryable), "Union",
            new Type[] { source.ElementType },
            source.Expression, other.Expression));
}

所以

var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);

应该真正编译和工作。

如果您需要Concat,请在上面添加类似的方法(基本上将Union替换为Concat

public static IQueryable Concat(this IQueryable source, IQueryable other)
{
    if (source == null) throw new ArgumentNullException("source");
    return source.Provider.CreateQuery(
        Expression.Call(
            typeof(Queryable), "Concat",
            new Type[] { source.ElementType },
            source.Expression, other.Expression));
}