开发者

LINQ equivalent of this query

开发者 https://www.devze.com 2023-03-05 14:33 出处:网络
SELECT StudentHistoryId, StudentId, Grade, CreatedAt, ModifiedAt, ModifiedBy, Active FROM TABLENAME TN
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


Here's a direct translaton:

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

精彩评论

暂无评论...
验证码 换一张
取 消