使用LINQ的IQueryable左外连接的扩展方法

25

我想实现一个左连接的扩展方法,返回值类型为IQueryable

我编写的函数如下:

public static IQueryable<TResult> LeftOuterJoin2<TOuter, TInner, TKey, TResult>(
        this IQueryable<TOuter> outer,
        IQueryable<TInner> inner,
        Func<TOuter, TKey> outerKeySelector,
        Func<TInner, TKey> innerKeySelector,
        Func<TOuter, TInner, TResult> resultSelector)
{
        return
          from outerItem in outer
          join innerItem in inner on outerKeySelector(outerItem) 
            equals innerKeySelector(innerItem) into joinedData
          from r in joinedData.DefaultIfEmpty()
          select resultSelector(outerItem, r);
}
它无法生成查询。可能的原因是:我使用了 Func<> 而不是 Expression<>。我也尝试过使用 Expression<>,但在 outerKeySelector(outerItem) 行上出现错误,这是因为 outerKeySelector 是一个用作方法的变量。
我在 SO(例如这里)和 CodeProjects 上找到了一些讨论,但那些仅适用于 IEnumerable 类型,而不适用于IQueryable

你收到了什么确切的错误信息?我的想法是 IQueryable 实际上 一个 IEnumerable,因此对于 IEnumerable 工作的方法也应该适用于这个实例,你尝试过使用适用于 IEnumerable 的方法,然后通过调用 .AsQueryable() 进行简单的强制转换为 IQueryable 吗? - aevitas
不同之处在于,IQueryable由查询提供程序转换为正确的SQL,然后针对数据库执行,而IEnumerable是LINQ to Objects的基础。IQueryable需要表达式树作为参数,IEnumerable则可以使用委托。 - MarcinJuraszek
6个回答

38

介绍

这个问题非常有趣。问题在于,Funcs是委托,而Expressions是树形结构,它们是完全不同的结构。当您使用当前的扩展实现时,它会在每个步骤中为每个元素执行选择器,并正常工作。但是,当我们谈论实体框架和LINQ时,需要进行树遍历以将其转换为SQL查询。因此,这比Funcs要“稍微”困难一些(但我仍然喜欢表达式),并且下面描述了一些问题。

当您想要进行左外连接时,可以使用类似于以下内容的代码(取自此处:如何在JOIN扩展方法中实现左连接

var leftJoin = p.Person.Where(n => n.FirstName.Contains("a"))
                   .GroupJoin(p.PersonInfo, 
                              n => n.PersonId,
                              m => m.PersonId,
                              (n, ms) => new { n, ms = ms.DefaultIfEmpty() })
                   .SelectMany(z => z.ms.Select(m => new { n = z.n, m ));

这很不错,但不是我们需要的扩展方法。我猜你需要像这样的东西:

using (var db = new Database1Entities("..."))
{
     var my = db.A.LeftOuterJoin2(db.B, a => a.Id, b => b.IdA, 
         (a, b) => new { a, b, hello = "Hello World!" });
     // other actions ...
}

创建这样的扩展程序有许多难点:

  • 需要手动创建复杂的树形结构,编译器无法帮助我们。
  • WhereSelect等方法需要使用反射。
  • 匿名类型(!! 这里需要代码生成吗?我希望不需要)。

步骤

考虑两个简单的表:A(列:Id、Text)和B(列:Id、IdA、Text)。

外连接可以分为3个步骤实现:

// group join as usual + use DefaultIfEmpty
var q1 = Queryable.GroupJoin(db.A, db.B, a => a.Id, b => b.IdA, 
                              (a, b) => new { a, groupB = b.DefaultIfEmpty() });

// regroup data to associated list a -> b, it is usable already, but it's 
// impossible to use resultSelector on this stage, 
// beacuse of type difference (quite deep problem: some anonymous type != TOuter)
var q2 = Queryable.SelectMany(q1, x => x.groupB, (a, b) => new { a.a, b });

// second regroup to get the right types
var q3 = Queryable.SelectMany(db.A, 
                               a => q2.Where(x => x.a == a).Select(x => x.b), 
                               (a, b) => new {a, b});

代码

好的,我不是一个很好的讲故事者,这里是我拥有的代码(抱歉我无法更好地格式化它,但它可以工作!):

public static IQueryable<TResult> LeftOuterJoin2<TOuter, TInner, TKey, TResult>(
        this IQueryable<TOuter> outer,
        IQueryable<TInner> inner,
        Expression<Func<TOuter, TKey>> outerKeySelector,
        Expression<Func<TInner, TKey>> innerKeySelector,
        Expression<Func<TOuter, TInner, TResult>> resultSelector)
    {

        // generic methods
        var selectManies = typeof(Queryable).GetMethods()
            .Where(x => x.Name == "SelectMany" && x.GetParameters().Length == 3)
            .OrderBy(x=>x.ToString().Length)
            .ToList();
        var selectMany = selectManies.First();
        var select = typeof(Queryable).GetMethods().First(x => x.Name == "Select" && x.GetParameters().Length == 2);
        var where = typeof(Queryable).GetMethods().First(x => x.Name == "Where" && x.GetParameters().Length == 2);
        var groupJoin = typeof(Queryable).GetMethods().First(x => x.Name == "GroupJoin" && x.GetParameters().Length == 5);
        var defaultIfEmpty = typeof(Queryable).GetMethods().First(x => x.Name == "DefaultIfEmpty" && x.GetParameters().Length == 1);

        // need anonymous type here or let's use Tuple
        // prepares for:
        // var q2 = Queryable.GroupJoin(db.A, db.B, a => a.Id, b => b.IdA, (a, b) => new { a, groupB = b.DefaultIfEmpty() });
        var tuple = typeof(Tuple<,>).MakeGenericType(
            typeof(TOuter),
            typeof(IQueryable<>).MakeGenericType(
                typeof(TInner)
                )
            );
        var paramOuter = Expression.Parameter(typeof(TOuter));
        var paramInner = Expression.Parameter(typeof(IEnumerable<TInner>));
        var groupJoinExpression = Expression.Call(
            null,
            groupJoin.MakeGenericMethod(typeof (TOuter), typeof (TInner), typeof (TKey), tuple),
            new Expression[]
                {
                    Expression.Constant(outer),
                    Expression.Constant(inner),
                    outerKeySelector,
                    innerKeySelector,
                    Expression.Lambda(
                        Expression.New(
                            tuple.GetConstructor(tuple.GetGenericArguments()),
                            new Expression[]
                                {
                                    paramOuter,
                                    Expression.Call(
                                        null,
                                        defaultIfEmpty.MakeGenericMethod(typeof (TInner)),
                                        new Expression[]
                                            {
                                                Expression.Convert(paramInner, typeof (IQueryable<TInner>))
                                            }
                                )
                                },
                            tuple.GetProperties()
                            ),
                        new[] {paramOuter, paramInner}
                )
                }
            );

        // prepares for:
        // var q3 = Queryable.SelectMany(q2, x => x.groupB, (a, b) => new { a.a, b });
        var tuple2 = typeof (Tuple<,>).MakeGenericType(typeof (TOuter), typeof (TInner));
        var paramTuple2 = Expression.Parameter(tuple);
        var paramInner2 = Expression.Parameter(typeof(TInner));
        var paramGroup = Expression.Parameter(tuple);
        var selectMany1Result = Expression.Call(
            null,
            selectMany.MakeGenericMethod(tuple, typeof (TInner), tuple2),
            new Expression[]
                {
                    groupJoinExpression,
                    Expression.Lambda(
                        Expression.Convert(Expression.MakeMemberAccess(paramGroup, tuple.GetProperty("Item2")),
                                           typeof (IEnumerable<TInner>)),
                        paramGroup
                ),
                    Expression.Lambda(
                        Expression.New(
                            tuple2.GetConstructor(tuple2.GetGenericArguments()),
                            new Expression[]
                                {
                                    Expression.MakeMemberAccess(paramTuple2, paramTuple2.Type.GetProperty("Item1")),
                                    paramInner2
                                },
                            tuple2.GetProperties()
                            ),
                        new[]
                            {
                                paramTuple2,
                                paramInner2
                            }
                )
                }
            );

        // prepares for final step, combine all expressinos together and invoke:
        // var q4 = Queryable.SelectMany(db.A, a => q3.Where(x => x.a == a).Select(x => x.b), (a, b) => new { a, b });
        var paramTuple3 = Expression.Parameter(tuple2);
        var paramTuple4 = Expression.Parameter(tuple2);
        var paramOuter3 = Expression.Parameter(typeof (TOuter));
        var selectManyResult2 = selectMany
            .MakeGenericMethod(
                typeof(TOuter),
                typeof(TInner),
                typeof(TResult)
            )
            .Invoke(
                null,
                new object[]
                    {
                        outer,
                        Expression.Lambda(
                            Expression.Convert(
                                Expression.Call(
                                    null,
                                    select.MakeGenericMethod(tuple2, typeof(TInner)),
                                    new Expression[]
                                        {
                                            Expression.Call(
                                                null,
                                                where.MakeGenericMethod(tuple2),
                                                new Expression[]
                                                    {
                                                        selectMany1Result,
                                                        Expression.Lambda( 
                                                            Expression.Equal(
                                                                paramOuter3,
                                                                Expression.MakeMemberAccess(paramTuple4, paramTuple4.Type.GetProperty("Item1"))
                                                            ),
                                                            paramTuple4
                                                        )
                                                    }
                                            ),
                                            Expression.Lambda(
                                                Expression.MakeMemberAccess(paramTuple3, paramTuple3.Type.GetProperty("Item2")),
                                                paramTuple3
                                            )
                                        }
                                ), 
                                typeof(IEnumerable<TInner>)
                            ),
                            paramOuter3
                        ),
                        resultSelector
                    }
            );

        return (IQueryable<TResult>)selectManyResult2;
    }

使用方法

再次使用:

db.A.LeftOuterJoin2(db.B, a => a.Id, b => b.IdA, 
       (a, b) => new { a, b, hello = "Hello World!" });

看到这个,你可能会想这一切的 SQL 查询语句会有多长?但是猜猜看,它其实很简短:

SELECT * FROM table_name;
SELECT 
1 AS [C1], 
[Extent1].[Id] AS [Id], 
[Extent1].[Text] AS [Text], 
[Join1].[Id1] AS [Id1], 
[Join1].[IdA] AS [IdA], 
[Join1].[Text2] AS [Text2], 
N'Hello World!' AS [C2]
FROM  [A] AS [Extent1]
INNER JOIN  (SELECT [Extent2].[Id] AS [Id2], [Extent2].[Text] AS [Text], [Extent3].[Id]    AS [Id1], [Extent3].[IdA] AS [IdA], [Extent3].[Text2] AS [Text2]
    FROM  [A] AS [Extent2]
    LEFT OUTER JOIN [B] AS [Extent3] ON [Extent2].[Id] = [Extent3].[IdA] ) AS [Join1] ON [Extent1].[Id] = [Join1].[Id2]
希望对您有所帮助。

这基本上也是LINQ“语言”的全部原因 - 一旦你进入连接,仅使用扩展方法做任何事情都非常痛苦。使用LINQ关键字方式可以产生更易于阅读的代码(即使在幕后执行相同的操作)。 - Luaan
4
我见过的最英勇的回答。 - spender

12

接受的答案非常好地解释了左外连接背后的复杂性。

我发现其中有三个相当严重的问题,特别是在将此扩展方法用于更复杂的查询时(链接多个左外连接和普通连接,然后进行汇总/最大值/计数等操作)。

在将所选答案复制到生产环境之前,请务必继续阅读。

考虑来自链接SO帖子的原始示例,它代表了LINQ中执行的几乎所有左外连接:

var leftJoin = p.Person.Where(n => n.FirstName.Contains("a"))
                   .GroupJoin(p.PersonInfo, 
                              n => n.PersonId,
                              m => m.PersonId,
                              (n, ms) => new { n, ms = ms })
                   .SelectMany(z => z.ms.DefaultIfEmpty(), (n, m) => new { n = n, m ));
  • The usage of a Tuple works, but when this is used as part of more complex queries, EF fails (cannot use constructors). To get around this, you either need to generate a new anonymous class dynamically (search stack overflow) or use a constructor-less type. I created this

    internal class KeyValuePairHolder<T1, T2>
    {
        public T1 Item1 { get; set; }
        public T2 Item2 { get; set; }
    }
    
  • The usage of the "Queryable.DefaultIfEmpty" method. In the original and in the GroupJoin methods, correct methods that are chosen by the compiler are the "Enumerable.DefaultIfEmpty" methods. This has no influence in a simple query, but notice how the accepted answer has a bunch of Converts (between IQueryable and IEnumerable). Those cast also cause issues in more complex queries. It's ok to use the "Enumerable.DefaultIfEmpty" method in an Expression, EF knows not to execute it but to translate it into a join instead.

  • Finally, this is the bigger issue: there are two selects done whereas the original only does one select. You can read the cause in the code comments (beacuse of type difference (quite deep problem: some anonymous type != TOuter)) and see it in the SQL (Select from A inner join (a left outer join b)) The issue here is that the Original SelectMany method takes an object created in the Join method of type: KeyValuePairHolder of TOuter and IEnumerable of Tinner as it's first parameter, but the resultSelector expression passed takes a simple TOUter as it's first parameter. You can use an ExpressionVisitor to rewrite the expression that is passed into the correct form.

    internal class ResultSelectorRewriter<TOuter, TInner, TResult> : ExpressionVisitor
    {
        private Expression<Func<TOuter, TInner, TResult>> resultSelector;
        public Expression<Func<KeyValuePairHolder<TOuter, IEnumerable<TInner>>, TInner, TResult>> CombinedExpression { get; private set; }
    
        private ParameterExpression OldTOuterParamExpression;
        private ParameterExpression OldTInnerParamExpression;
        private ParameterExpression NewTOuterParamExpression;
        private ParameterExpression NewTInnerParamExpression;
    
    
        public ResultSelectorRewriter(Expression<Func<TOuter, TInner, TResult>> resultSelector)
        {
            this.resultSelector = resultSelector;
            this.OldTOuterParamExpression = resultSelector.Parameters[0];
            this.OldTInnerParamExpression = resultSelector.Parameters[1];
    
            this.NewTOuterParamExpression = Expression.Parameter(typeof(KeyValuePairHolder<TOuter, IEnumerable<TInner>>));
            this.NewTInnerParamExpression = Expression.Parameter(typeof(TInner));
    
            var newBody = this.Visit(this.resultSelector.Body);
            var combinedExpression = Expression.Lambda(newBody, new ParameterExpression[] { this.NewTOuterParamExpression, this.NewTInnerParamExpression });
            this.CombinedExpression = (Expression<Func<KeyValuePairHolder<TOuter, IEnumerable<TInner>>, TInner, TResult>>)combinedExpression;
        }
    
    
        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (node == this.OldTInnerParamExpression)
                return this.NewTInnerParamExpression;
            else if (node == this.OldTOuterParamExpression)
                return Expression.PropertyOrField(this.NewTOuterParamExpression, "Item1");
            else
                throw new InvalidOperationException("What is this sorcery?", new InvalidOperationException("Did not expect a parameter: " + node));
    
        } 
    }
    

使用表达式访问者和KeyValuePairHolder来避免使用元组,我的更新版本修复了三个问题,更短,生成的SQL也更短:

 internal class QueryReflectionMethods
    {
        internal static System.Reflection.MethodInfo Enumerable_Select = typeof(Enumerable).GetMethods().First(x => x.Name == "Select" && x.GetParameters().Length == 2);
        internal static System.Reflection.MethodInfo Enumerable_DefaultIfEmpty = typeof(Enumerable).GetMethods().First(x => x.Name == "DefaultIfEmpty" && x.GetParameters().Length == 1);

        internal static System.Reflection.MethodInfo Queryable_SelectMany = typeof(Queryable).GetMethods().Where(x => x.Name == "SelectMany" && x.GetParameters().Length == 3).OrderBy(x => x.ToString().Length).First();
        internal static System.Reflection.MethodInfo Queryable_Where = typeof(Queryable).GetMethods().First(x => x.Name == "Where" && x.GetParameters().Length == 2);
        internal static System.Reflection.MethodInfo Queryable_GroupJoin = typeof(Queryable).GetMethods().First(x => x.Name == "GroupJoin" && x.GetParameters().Length == 5);
        internal static System.Reflection.MethodInfo Queryable_Join = typeof(Queryable).GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).First(c => c.Name == "Join");
        internal static System.Reflection.MethodInfo Queryable_Select = typeof(Queryable).GetMethods().First(x => x.Name == "Select" && x.GetParameters().Length == 2);



        public static IQueryable<TResult> CreateLeftOuterJoin<TOuter, TInner, TKey, TResult>(
                   IQueryable<TOuter> outer,
                   IQueryable<TInner> inner,
                   Expression<Func<TOuter, TKey>> outerKeySelector,
                   Expression<Func<TInner, TKey>> innerKeySelector,
                   Expression<Func<TOuter, TInner, TResult>> resultSelector)
        { 

            var keyValuePairHolderWithGroup = typeof(KeyValuePairHolder<,>).MakeGenericType(
                typeof(TOuter),
                typeof(IEnumerable<>).MakeGenericType(
                    typeof(TInner)
                    )
                );
            var paramOuter = Expression.Parameter(typeof(TOuter));
            var paramInner = Expression.Parameter(typeof(IEnumerable<TInner>));
            var groupJoin =
                Queryable_GroupJoin.MakeGenericMethod(typeof(TOuter), typeof(TInner), typeof(TKey), keyValuePairHolderWithGroup)
                .Invoke(
                    "ThisArgumentIsIgnoredForStaticMethods",
                    new object[]{
                    outer,
                    inner,
                    outerKeySelector,
                    innerKeySelector,
                    Expression.Lambda(
                        Expression.MemberInit(
                            Expression.New(keyValuePairHolderWithGroup), 
                            Expression.Bind(
                                keyValuePairHolderWithGroup.GetMember("Item1").Single(),  
                                paramOuter
                                ), 
                            Expression.Bind(
                                keyValuePairHolderWithGroup.GetMember("Item2").Single(), 
                                paramInner
                                )
                            ),
                        paramOuter, 
                        paramInner
                        )
                    }
                );


            var paramGroup = Expression.Parameter(keyValuePairHolderWithGroup);
            Expression collectionSelector = Expression.Lambda(                    
                            Expression.Call(
                                    null,
                                    Enumerable_DefaultIfEmpty.MakeGenericMethod(typeof(TInner)),
                                    Expression.MakeMemberAccess(paramGroup, keyValuePairHolderWithGroup.GetProperty("Item2"))) 
                            ,
                            paramGroup
                        );

            Expression newResultSelector = new ResultSelectorRewriter<TOuter, TInner, TResult>(resultSelector).CombinedExpression;


            var selectMany1Result =
                Queryable_SelectMany.MakeGenericMethod(keyValuePairHolderWithGroup, typeof(TInner), typeof(TResult))
                .Invoke(
                    "ThisArgumentIsIgnoredForStaticMethods", new object[]{
                        groupJoin,
                        collectionSelector,
                        newResultSelector
                    }
                );
            return (IQueryable<TResult>)selectMany1Result;
        }
    }

虽然您的方法似乎适用于EF6,但我尝试使用您建议的方法来处理EF Core 2.0,但未能获得结果。我不确定这是否是EF Core 2.0的错误。我在这里提出了问题:https://stackoverflow.com/questions/46537158/trying-to-implement-a-leftjoin-extension-method-to-work-with-ef-core-2-0 - Sudarsha Hewa

9
如之前的答案所述,当你想要将IQueryable翻译成SQL时,需要使用Expression而不是Func,因此你必须走Expression Tree的路线。
然而,这里有一种方法可以在不必自己构建Expression tree的情况下实现相同的结果。诀窍是,你需要引用LinqKit(可通过NuGet获得),并在查询上调用AsExpandable()。这将负责构建基础表达式树(请参见here)。
下面的示例使用GroupJoinSelectManyDefaultIfEmpty()方法: 代码
    public static IQueryable<TResult> LeftOuterJoin<TOuter, TInner, TKey, TResult>(
        this IQueryable<TOuter> outer,
        IQueryable<TInner> inner,
        Expression<Func<TOuter, TKey>> outerKeySelector,
        Expression<Func<TInner, TKey>> innerKeySelector,
        Expression<Func<TOuter, TInner, TResult>> resultSelector)
    {
        return outer
            .AsExpandable()// Tell LinqKit to convert everything into an expression tree.
            .GroupJoin(
                inner,
                outerKeySelector,
                innerKeySelector,
                (outerItem, innerItems) => new { outerItem, innerItems })
            .SelectMany(
                joinResult => joinResult.innerItems.DefaultIfEmpty(),
                (joinResult, innerItem) => 
                    resultSelector.Invoke(joinResult.outerItem, innerItem));
    }

样例数据

假设我们有以下EF实体,而usersaddresses变量是访问底层DbSet的方式:

public class User
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class UserAddress
{
    public int UserId { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
}

IQueryable<User> users;
IQueryable<UserAddress> addresses;

用法 1

让我们通过用户 ID 加入:

var result = users.LeftOuterJoin(
            addresses,
            user => user.Id,
            address => address.UserId,
            (user, address) => new { user.Id, address.Street });

这个翻译为(使用 LinqPad):

SELECT 
[Extent1].[Id] AS [Id],     
[Extent2].[Street] AS [Street]
FROM  [dbo].[Users] AS [Extent1]
LEFT OUTER JOIN [dbo].[UserAddresses] AS [Extent2] 
ON [Extent1].[Id] = [Extent2].[UserId]

用法 2

现在让我们使用匿名类型作为键来连接多个属性:

var result = users.LeftOuterJoin(
            addresses,
            user => new { user.Id, user.LastName },
            address => new { Id = address.UserId, address.LastName },
            (user, address) => new { user.Id, address.Street });

请注意,匿名类型的属性必须具有相同的名称,否则会出现语法错误。
这就是为什么我们使用 Id = address.UserId 而不是仅仅使用 address.UserId 的原因。
这将被翻译为:
SELECT 
[Extent1].[Id] AS [Id],     
[Extent2].[Street] AS [Street]
FROM  [dbo].[Users] AS [Extent1]
LEFT OUTER JOIN [dbo].[UserAddresses] AS [Extent2] 
ON ([Extent1].[Id] = [Extent2].[UserId]) AND ([Extent1].[LastName] = [Extent2].[LastName])

4
这是我去年创建的.LeftJoin扩展方法,当时我想简化.GroupJoin。 我已经成功地使用它了。 我包含了XML注释,这样你就可以获得完整的智能感知。此外还有一个带IEqualityComparer的重载。 希望你觉得它有用。
我的完整Join扩展套件在这里:https:// github.com / jolsa / Extensions / blob / master / ExtensionLib / JoinExtensions.cs
// JoinExtensions: Created 07/12/2014 - Johnny Olsa

using System.Linq;

namespace System.Collections.Generic
{
    /// <summary>
    /// Join Extensions that .NET should have provided?
    /// </summary>
    public static class JoinExtensions
    {
        /// <summary>
        /// Correlates the elements of two sequences based on matching keys. A specified
        /// System.Collections.Generic.IEqualityComparer&lt;T&gt; is used to compare keys.
        /// </summary>
        /// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
        /// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
        /// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
        /// <typeparam name="TResult">The type of the result elements.</typeparam>
        /// <param name="outer">The first sequence to join.</param>
        /// <param name="inner">The sequence to join to the first sequence.</param>
        /// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
        /// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
        /// <param name="resultSelector">A function to create a result element from two combined elements.</param>
        /// <param name="comparer">A System.Collections.Generic.IEqualityComparer&lt;T&gt; to hash and compare keys.</param>
        /// <returns>
        /// An System.Collections.Generic.IEnumerable&lt;T&gt; that has elements of type TResult
        /// that are obtained by performing an left outer join on two sequences.
        /// </returns>
        /// <example>
        /// Example:
        /// <code>
        /// class TestClass
        /// {
        ///        static int Main()
        ///        {
        ///            var strings1 = new string[] { "1", "2", "3", "4", "a" };
        ///            var strings2 = new string[] { "1", "2", "3", "16", "A" };
        ///            
        ///            var lj = strings1.LeftJoin(
        ///                strings2,
        ///                a => a,
        ///                b => b,
        ///                (a, b) => (a ?? "null") + "-" + (b ?? "null"),
        ///                StringComparer.OrdinalIgnoreCase)
        ///                .ToList();
        ///        }
        ///    }
        ///    </code>
        /// </example>
        public static IEnumerable<TResult> LeftJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
            IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector,
            Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
        {
            return outer.GroupJoin(
                inner,
                outerKeySelector,
                innerKeySelector,
                (o, ei) => ei
                    .Select(i => resultSelector(o, i))
                    .DefaultIfEmpty(resultSelector(o, default(TInner))), comparer)
                    .SelectMany(oi => oi);
        }

        /// <summary>
        /// Correlates the elements of two sequences based on matching keys. The default
        /// equality comparer is used to compare keys.
        /// </summary>
        /// <typeparam name="TOuter">The type of the elements of the first sequence.</typeparam>
        /// <typeparam name="TInner">The type of the elements of the second sequence.</typeparam>
        /// <typeparam name="TKey">The type of the keys returned by the key selector functions.</typeparam>
        /// <typeparam name="TResult">The type of the result elements.</typeparam>
        /// <param name="outer">The first sequence to join.</param>
        /// <param name="inner">The sequence to join to the first sequence.</param>
        /// <param name="outerKeySelector">A function to extract the join key from each element of the first sequence.</param>
        /// <param name="innerKeySelector">A function to extract the join key from each element of the second sequence.</param>
        /// <param name="resultSelector">A function to create a result element from two combined elements.</param>
        /// <returns>
        /// An System.Collections.Generic.IEnumerable&lt;T&gt; that has elements of type TResult
        /// that are obtained by performing an left outer join on two sequences.
        /// </returns>
        /// <example>
        /// Example:
        /// <code>
        /// class TestClass
        /// {
        ///        static int Main()
        ///        {
        ///            var strings1 = new string[] { "1", "2", "3", "4", "a" };
        ///            var strings2 = new string[] { "1", "2", "3", "16", "A" };
        ///            
        ///            var lj = strings1.LeftJoin(
        ///                strings2,
        ///                a => a,
        ///                b => b,
        ///                (a, b) => (a ?? "null") + "-" + (b ?? "null"))
        ///                .ToList();
        ///        }
        ///    }
        ///    </code>
        /// </example>
        public static IEnumerable<TResult> LeftJoin<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
            IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector,
            Func<TOuter, TInner, TResult> resultSelector)
        {
            return outer.LeftJoin(inner, outerKeySelector, innerKeySelector, resultSelector, default(IEqualityComparer<TKey>));
        }

    }
}

我正在使用你的LeftJoin扩展方法,但在将它们链接在一起时出现了空引用异常。 - Justin
能否修改代码以接受 parentkey、childkey 和 selector 的字符串形式作为参数? - Asım Gündüz
@Licentia,你能给我一个你想要做的例子吗?也许我可以帮忙。 - JohnnyIV
@JohnnyIV,有一个动态Linq库,你可以从NuGet获取:System.linq.dynamic.core。这个库允许你以这种方式使用扩展方法=>foo.Join(bar,"fooId","barId", "new(outer.fooId, outer.fooName, inner.barId, inner.bar...)"); 这将产生一个内连接...我想知道是否可能以这种方式进行左外连接。 - Asım Gündüz
@Licentia,我之前并不了解这个库。它似乎可以在运行时将字符串解析为字段或属性。我可以看到一些这样使用的场景,但目前我还没有需要这种灵活性的情况。这对于.OrderBy来说是有用的。我的第一反应是反编译它以查看它的工作原理。它肯定使用了反射来实现此功能。我可能会尝试找到一种简单的方法来做到这一点。 - JohnnyIV
显示剩余4条评论

0
我的前一个回答需要更新。当我发布它时,我没有注意到问题是关于转换为SQL的。这段代码适用于本地项目,因此对象将首先被提取,然后再连接,而不是在服务器上执行外部连接。但是,为了使用我之前发布的Join扩展处理空值,这里有一个示例:
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class EmailAddress
{
    public int Id { get; set; }
    public Email Email { get; set; }
}
public class Email
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public static void Main()
{
    var people = new []
    {
        new Person() { Id = 1, Name = "John" },
        new Person() { Id = 2, Name = "Paul" },
        new Person() { Id = 3, Name = "George" },
        new Person() { Id = 4, Name = "Ringo" }
    };
    var addresses = new[]
    {
        new EmailAddress() { Id = 2, Email = new Email() { Name = "Paul", Address = "Paul@beatles.com" } },
        new EmailAddress() { Id = 3, Email = new Email() { Name = "George", Address = "George@beatles.com" } },
        new EmailAddress() { Id = 4, Email = new Email() { Name = "Ringo", Address = "Ringo@beatles.com" } }
    };

    var joinedById = people.LeftJoin(addresses, p => p.Id, a => a.Id, (p, a) => new
    {
        p.Id,
        p.Name,
        a?.Email.Address
    }).ToList();

    Console.WriteLine("\r\nJoined by Id:\r\n");
    joinedById.ForEach(j => Console.WriteLine($"{j.Id}-{j.Name}: {j.Address ?? "<null>"}"));

    var joinedByName = people.LeftJoin(addresses, p => p.Name, a => a?.Email.Name, (p, a) => new
    {
        p.Id,
        p.Name,
        a?.Email.Address
    }, StringComparer.OrdinalIgnoreCase).ToList();

    Console.WriteLine("\r\nJoined by Name:\r\n");
    joinedByName.ForEach(j => Console.WriteLine($"{j.Id}-{j.Name}: {j.Address ?? "<null>"}"));

}

@RaduV提供了一个出色的处理服务器连接的解决方案。我尝试过并且很喜欢它。我会补充一点,如果可能的话,我更喜欢使用IEnumerable<T>连接,因为你不受限于与数据库兼容的语法。但是,为了提高性能并限制要处理的数据量,将内部/外部连接放在服务器上执行是有益的。 - JohnnyIV

0
@Licentia,这是我为解决您的问题所想出的方法。我创建了DynamicJoinDynamicLeftJoin扩展方法,类似于您向我展示的内容,但我处理输出方式不同,因为字符串解析容易出现许多问题。这不会连接匿名类型,但您可以调整它以实现此功能。它也没有IComparable的重载,但很容易添加。属性名称必须与类型大小写相同。这与我上面的扩展方法同时使用(即没有它们无法工作)。希望对您有所帮助!
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class EmailAddress
{
    public int PersonId { get; set; }
    public Email Email { get; set; }
}
public class Email
{
    public string Name { get; set; }
    public string Address { get; set; }
}

public static void Main()
{
    var people = new[]
    {
        new Person() { Id = 1, Name = "John" },
        new Person() { Id = 2, Name = "Paul" },
        new Person() { Id = 3, Name = "George" },
        new Person() { Id = 4, Name = "Ringo" }
    };
    var addresses = new[]
    {
        new EmailAddress() { PersonId = 2, Email = new Email() { Name = "Paul", Address = "Paul@beatles.com" } },
        new EmailAddress() { PersonId = 3, Email = new Email() { Name = "George", Address = "George@beatles.com" } },
        new EmailAddress() { PersonId = 4, Email = new Email() { Name = "Ringo" } }
    };

    Console.WriteLine("\r\nInner Join:\r\n");
    var innerJoin = people.DynamicJoin(addresses, "Id", "PersonId", "outer.Id", "outer.Name", "inner.Email").ToList();
    innerJoin.ForEach(j => Console.WriteLine($"{j.Id}-{j.Name}: {j?.Email?.Address ?? "<null>"}"));

    Console.WriteLine("\r\nOuter Join:\r\n");
    var leftJoin = people.DynamicLeftJoin(addresses, "Id", "PersonId", "outer.Id", "outer.Name", "inner.Email").ToList();
    leftJoin.ForEach(j => Console.WriteLine($"{j.Id}-{j.Name}: {j?.Email?.Address ?? "<null>"}"));

}

public static class DynamicJoinExtensions
{
    private const string OuterPrefix = "outer.";
    private const string InnerPrefix = "inner.";

    private class Processor<TOuter, TInner>
    {
        private readonly Type _typeOuter = typeof(TOuter);
        private readonly Type _typeInner = typeof(TInner);
        private readonly PropertyInfo _keyOuter;
        private readonly PropertyInfo _keyInner;
        private readonly List<string> _outputFields;
        private readonly Dictionary<string, PropertyInfo> _resultProperties;

        public Processor(string outerKey, string innerKey, IEnumerable<string> outputFields)
        {
            _outputFields = outputFields.ToList();

            //  Check for properties with the same name
            string badProps = string.Join(", ", _outputFields.Select(f => new { property = f, name = GetName(f) })
                .GroupBy(f => f.name, StringComparer.OrdinalIgnoreCase)
                .Where(g => g.Count() > 1)
                .SelectMany(g => g.OrderBy(f => f.name, StringComparer.OrdinalIgnoreCase).Select(f => f.property)));
            if (!string.IsNullOrEmpty(badProps))
                throw new ArgumentException($"One or more {nameof(outputFields)} are duplicated: {badProps}");

            _keyOuter = _typeOuter.GetProperty(outerKey);
            _keyInner = _typeInner.GetProperty(innerKey);

            //  Check for valid keys
            if (_keyOuter == null || _keyInner == null)
                throw new ArgumentException($"One or both of the specified keys is not a valid property");

            //  Check type compatibility
            if (_keyOuter.PropertyType != _keyInner.PropertyType)
                throw new ArgumentException($"Keys must be the same type. ({nameof(outerKey)} type: {_keyOuter.PropertyType.Name}, {nameof(innerKey)} type: {_keyInner.PropertyType.Name})");

            Func<string, Type, IEnumerable<KeyValuePair<string, PropertyInfo>>> getResultProperties = (prefix, type) =>
               _outputFields.Where(f => f.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
                   .Select(f => new KeyValuePair<string, PropertyInfo>(f, type.GetProperty(f.Substring(prefix.Length))));

            //  Combine inner/outer outputFields with PropertyInfo into a dictionary
            _resultProperties = getResultProperties(OuterPrefix, _typeOuter).Concat(getResultProperties(InnerPrefix, _typeInner))
                .ToDictionary(k => k.Key, v => v.Value, StringComparer.OrdinalIgnoreCase);

            //  Check for properties that aren't found
            badProps = string.Join(", ", _resultProperties.Where(kv => kv.Value == null).Select(kv => kv.Key));
            if (!string.IsNullOrEmpty(badProps))
                throw new ArgumentException($"One or more {nameof(outputFields)} are not valid: {badProps}");

            //  Check for properties that aren't the right format
            badProps = string.Join(", ", _outputFields.Where(f => !_resultProperties.ContainsKey(f)));
            if (!string.IsNullOrEmpty(badProps))
                throw new ArgumentException($"One or more {nameof(outputFields)} are not valid: {badProps}");

        }
        //  Inner Join
        public IEnumerable<dynamic> Join(IEnumerable<TOuter> outer, IEnumerable<TInner> inner) =>
            outer.Join(inner, o => GetOuterKeyValue(o), i => GetInnerKeyValue(i), (o, i) => CreateItem(o, i));
        //  Left Outer Join
        public IEnumerable<dynamic> LeftJoin(IEnumerable<TOuter> outer, IEnumerable<TInner> inner) =>
            outer.LeftJoin(inner, o => GetOuterKeyValue(o), i => GetInnerKeyValue(i), (o, i) => CreateItem(o, i));

        private static string GetName(string fieldId) => fieldId.Substring(fieldId.IndexOf('.') + 1);
        private object GetOuterKeyValue(TOuter obj) => _keyOuter.GetValue(obj);
        private object GetInnerKeyValue(TInner obj) => _keyInner.GetValue(obj);
        private object GetResultProperyValue(string key, object obj) => _resultProperties[key].GetValue(obj);
        private dynamic CreateItem(TOuter o, TInner i)
        {
            var obj = new ExpandoObject();
            var dict = (IDictionary<string, object>)obj;
            _outputFields.ForEach(f =>
            {
                var source = f.StartsWith(OuterPrefix, StringComparison.OrdinalIgnoreCase) ? (object)o : i;
                dict.Add(GetName(f), source == null ? null : GetResultProperyValue(f, source));
            });
            return obj;
        }
    }

    public static IEnumerable<dynamic> DynamicJoin<TOuter, TInner>(this IEnumerable<TOuter> outer,
            IEnumerable<TInner> inner, string outerKey, string innerKey,
            params string[] outputFields) =>
        new Processor<TOuter, TInner>(outerKey, innerKey, outputFields).Join(outer, inner);
    public static IEnumerable<dynamic> DynamicLeftJoin<TOuter, TInner>(this IEnumerable<TOuter> outer,
            IEnumerable<TInner> inner, string outerKey, string innerKey,
            params string[] outputFields) =>
        new Processor<TOuter, TInner>(outerKey, innerKey, outputFields).LeftJoin(outer, inner);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接