Linq离开了连接语法修正所需

时间:2012-07-17 20:46:52

标签: c# linq

我在找出多个连接中左外连接的Linq语法时遇到问题。我想在RunLogEntry表上进行左连接,这样我就可以获得与此表以及所有服务条目匹配的记录。

有人可以纠正我的snytax吗?

 var list = (from se in db.ServiceEntry
    join u in db.User on se.TechnicianID equals u.ID
    join s in db.System1 on se.SystemID equals s.ID
    join r in db.RunLogEntry on se.RunLogEntryID equals r.ID
    where se.ClosedDate.HasValue == false
    where se.ClosedDate.HasValue == false
        && se.Reconciled == false
    orderby se.ID descending
    select new ServiceSearchEntry()
    {
        ID = se.ID,
        ServiceDateTime = se.ServiceDateTime,
        Technician = u.FullName,
        System = s.SystemFullName,
        ReasonForFailure = se.ReasonForFailure,
        RunDate = r.RunDate
    })
    .Skip((page - 1) * PageSize);

1 个答案:

答案 0 :(得分:5)

使用.DefaultIfEmpty上的r方法执行左连接:

var list = (from se in db.ServiceEntry
    join r in db.RunLogEntry on se.RunLogEntryID equals r.ID into joinRunLogEntry
    from r2 in joinRunLogEntry.DefaultIfEmpty()
    join u in db.User on se.TechnicianID equals u.ID
    join s in db.System1 on se.SystemID equals s.ID
    where se.ClosedDate.HasValue == false
    where se.ClosedDate.HasValue == false
        && se.Reconciled == false
    orderby se.ID descending
    select new ServiceSearchEntry()
    {
        ID = se.ID,
        ServiceDateTime = se.ServiceDateTime,
        Technician = u.FullName,
        System = s.SystemFullName,
        ReasonForFailure = se.ReasonForFailure,
        RunDate = (r2 == null ? (DateTime?)null : r2.RunDate)
    })
    .Skip((page - 1) * PageSize);