Linq Join:无法基于2个id连接2个List

本文关键字:2个 id 连接 List Join Linq | 更新日期: 2023-09-27 17:53:05

我有两个表AtdDailyAttendanceAcdAdmissionSessionDetails。我想从linq中基于两个id连接这两个表,这意味着在Sql中这个连接看起来像

SELECT a.Id, a.DateId,  
    a.StudentLedgerId, a.AttendanceTypeId,   
    a.SchoolId, a.UserId, a.SessionId, 
    d.ClassId,d.MediumId,    
    d.StreamId, d.ShiftId,  
    d.SectionId   
FROM AtdDailyAttendance a INNER JOIN  AcdAdmissionSessionDetail d
    ON a.StudentLedgerId = d.StudentLedgerId AND  a.SessionId = d.SessionId

但是在LINQ中我无法做到这一点。我试过这个方法

var query =
    from a in dbCOntext.AtdDailyAttendances
    join b in dbCOntext.AcdAdmissionSessionDetails
    on a.StudentLedgerId equals b.StudentLedgerId
    // on a.SessionId equlas b.SessionId
    select new
    {
        a.AtdSetedDatesForAttendance,
        a.DateId,
        a.StudentLedgerId,
        a.SchoolId,
        a.UserId,
        a.SessionId,
        b.ClassId,
        b.SectionId,
        b.MediumId,
        b.StreamId
    }
var liResult = query.ToList();

这里我无法执行SessionId之间的连接

Linq Join:无法基于2个id连接2个List

我想你要找的是:

var liResult = (from a in dbCOntext.AtdDailyAttendances
  join b in dbCOntext.AcdAdmissionSessionDetails
  on new { a.StudentLedgerId, a.SessionId } 
     equals new { b.StudentLedgerId, b.SessionId}

看看是否可以:

 dbContext.AtdDailyAttendances.Join(dbContext.AcAdmissionSessionDetails, m => m.StudentLedgerId,      n => n.StudentLedgerId, (m, n) => new{ 
                                m.AcdAdmissionSessionDetails,
                                m.DateId,
                                m.StudentLedgerId,
                                m.SchoolId,
                                m.UserId,
                                AttendanceSessionId = n.SessionId,
                                AdmissionSessionId = m.SessionId,
                                n.ClassId,
                                n.SectionId,
                                n.MediumId,
                                n.StreamId
  }).Where(n => n.AttendanceSessionId == n.AdmissionSessionId).ToList();