首页 新闻 赞助 找找看

linq 左连接 原理 有人解释一下原理吗?

0
[待解决问题]

public class As
{
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
}
public class Asd
{
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
}

        List<As> ass = new List<As> { 
            new As{a="1",b="b1",c="c1"},
            new As{a="2",b="b2",c="c3"},
            new As{a="3",b="b3",c="c3"},
            new As{a="4",b="b4",c="c4"},
            new As{a="5",b="b5",c="c5"},
        };

        List<Asd> asds = new List<Asd> { 
            new Asd{a="1",b="bb1",c="cc1"},
            new Asd{a="2",b="bb2",c="cc2"},
            new Asd{a="3",b="bb3",c="cc3"},
        };

var sdsdssleft = from a in ass
join b in asds
on a.a equals b.a
into tmp
from c in tmp.DefaultIfEmpty()
select new
{
aa = a.a,
ab = a.b,
ac = a.c,
ba = c != null ? c.a : null,
bb = c != null ? c.b : null,
bc = c != null ? c.c : null,
};

能实现左链接
问题1 into tmp ( tmp 是一个Asd集合) into 不应该是 临时结果标识吗?
按道理应该是 第一个对象 和 第二个对象join后的集合呀? 为什么是第二个对象?
那么tmp里面具体数据是什么?

问题2 tmp.DefaultIfEmpty() //返回指定序列的元素;如果序列为空,则返回单一实例集合中的类型参数的默认值。
试了一下
var rr = ass.DefaultIfEmpty(); 代码可以直接这么写 rr ass 两者没差异
只有当ass长度为0时 rr才为长度为1的 空As对象
from c in tmp.DefaultIfEmpty() 中 tmp 既然为Asd的集合 返回的话 也是原数据 应该写了也相当于没写呀?

问题3 希望有了解底层的帮我解释解释这里面代码执行过程 为什么会实现左链接?

~雨落忧伤~的主页 ~雨落忧伤~ | 菜鸟二级 | 园豆:336
提问于:2019-01-04 16:08

同问

想怎样逗随便 4年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册