EF核心从孩子到父母的左连接 [英] EF core left join from child to parent

查看:42
本文介绍了EF核心从孩子到父母的左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以不让孩子从父母那里加入吗?

Can we left join from child to parent ?

 var query = Child.Join(Context.Set<Parent>(), r => r.ParentId, u => u.Id,
            (r, u) => new
            {
                Parent= u,
                Child= r
            });

这被翻译为inner-join,但是我想左连接

This is translate to inner-join , but I want left join

有可能吗?

推荐答案

使用LINQ查询语法更好地做到这一点:

Better to do that with LINQ Query syntax:

var query =
    from c in Context.Set<Child>() 
    join p in Context.Set<Parent>() on c.ParentId equals p.Id into gj
    from p in gj.DefaultIfEmpty()
    select new 
    {
        Parent = p,
        Child = c
    };

这篇关于EF核心从孩子到父母的左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆