加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在EF中指定左连接?

发布时间:2020-12-16 00:21:26 所属栏目:百科 来源:网络整理
导读:我的模特: public partial class history_builds{ public int ID { get; set; } public int build { get; set; } public int br { get; set; } public int tag { get; set; } public string line { get; set; } public int rev { get; set; } public int us
我的模特:

public partial class history_builds
{
    public int ID { get; set; }
    public int build { get; set; }
    public int br { get; set; }
    public int tag { get; set; }
    public string line { get; set; }
    public int rev { get; set; }
    public int user_ID { get; set; }
    public string distrib_path { get; set; }
    public string setup_path { get; set; }
    public System.DateTime build_date { get; set; }
    public string product { get; set; }
}

public partial class history_uploads
{
    public int ID { get; set; }
    public int ID_user { get; set; }
    public string Line { get; set; }
    public int Build { get; set; }
    public string Distrib { get; set; }
    public System.DateTime Time_upload { get; set; }
    public int Status { get; set; }
}

public partial class user
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int ID_group { get; set; }
}

语境:

public DbSet<history_builds> history_builds { get; set; }
public DbSet<history_uploads> history_uploads { get; set; }
public DbSet<user> users { get; set; }

我试着做这个Entity framework left join的左连接

var items = entities.history_builds
    .Join(
        entities.users.DefaultIfEmpty(),hb => hb.user_ID,u => u.ID,(hb,u) =>
            new {
                hb.distrib_path,hb.setup_path,hb.build_date,hb.build,User = (u == null ? String.Empty : u.Name),hb.rev
            }
    )
    .Join(entities.history_uploads.DefaultIfEmpty(),hb => hb.build,hu => hu.Build,hu) =>
            new HistoryBuidItem {
                Revision = hb.rev,Build = hb.build,DistribPath = hb.distrib_path,SetupPath = hb.setup_path,BuildDate = hb.build_date,User = hb.User,IsUpload = (hu == null ? true : false)
            }
    )
    .Where(x => ids.Contains(x.Revision))
    .ToList();

但它不起作用,EF仍然发出内连接sql代码,有什么问题?

解决方法

左外连接示例:

from c in table0
join o in table1 on c.sno equals o.sno into ps
from o in ps.DefaultIfEmpty()
select new { c.name,o.number}

它渲染SQL:

SELECT [t0].[name],[t1].[number] AS [number]
FROM [table0] AS [t0]
LEFT OUTER JOIN [table1] AS [t1] ON ([t0].[sno]) = [t1].[sno]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读