LINQ相当于这个查询

时间:2011-05-13 19:55:36

标签: linq

SELECT StudentHistoryId, StudentId, Grade, CreatedAt, ModifiedAt, ModifiedBy, Active
FROM TABLENAME TN
     INNER JOIN ( SELECT StudentId, MAX(ModifiedAt) AS ModifiedAt FROM TABLENAME GROUP BY StudentId) M
          ON TN.StudentId = M.StudentId AND TN.ModifiedAt = M.ModifiedAt

1 个答案:

答案 0 :(得分:0)

这是一个直接的翻译:

var subquery = from tn in dc.TABLENAME
               group tn by tn.StudentId into g
               select new { StudentId = g.Key, ModifiedAt = g.Max(x => x.ModifiedAt) };
var query = from tn in dc.TABLENAME
            join m in subquery
                on new { tn.StudentId, tn.ModifiedAt }
                equals new { m.StudentId, m.ModifiedAt }
            select new
            {
                tn.StudentHistoryId,
                tn.StudentId,
                tn.Grade,
                tn.CreatedAt,
                tn.ModifiedAt,
                tn.ModifiedBy,
                tn.Active
            };