【问题标题】:EF core left join from child to parentEF核心左连接从孩子到父母
【发布时间】:2021-02-11 09:12:50
【问题描述】:

我们可以从孩子离开加入父母吗?

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

这是转换为 inner-join ,但我想要左连接

有可能吗?

【问题讨论】:

    标签: entity-framework entity-framework-core


    【解决方案1】:

    最好使用 LINQ 查询语法:

    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
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 2023-02-26
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多