csharp LINQ中的左外连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp LINQ中的左外连接相关的知识,希望对你有一定的参考价值。

void Main()
{
    Dictionary<int, string> mere = new Dictionary<int, string>();
    mere.Add(1, "rosii");
    mere.Add(2, "verzi");
    mere.Add(3, "albastre");
    mere.Add(4, "galbene");

    Dictionary<int, string> pere = new Dictionary<int, string>();
    pere.Add(3, "albastre");
    pere.Add(4, "galbene");

    //use only in memory
    var joined = mere.GroupJoin(pere, x => x.Key, x => x.Key, (x, y) => new { Mere = x, Pere= y })
    	.Select(x => new { Mar=x.Mere, Par = x.Pere.DefaultIfEmpty().FirstOrDefault()});
    //good sql
    var joined2 = mere.GroupJoin(pere, x => x.Key, x => x.Key, (x, y) => new { Mere = x, Pere = y })
     	.SelectMany(x => x.Pere.DefaultIfEmpty(), (x, y) => new { Mar = x.Mere, Par = y });
    //good sql
    var joined3 =
        from c in mere
        join p in pere on c.Key equals p.Key into ps
        from p in ps.DefaultIfEmpty()
        select new { x = c, y=p };
	
    joined.Dump();
    joined2.Dump();
    joined3.Dump();
}

以上是关于csharp LINQ中的左外连接的主要内容,如果未能解决你的问题,请参考以下文章

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

Linq中的lambda /方法语法中的左外连接[重复]

Linq中的左外连接

LINQ:具有多个条件的左外连接

将两个查询的左外连接转换为 LINQ

使用 LINQ 查询语法 EF Core C# 的左外连接