Linq to sql 等效LINQ到SQL查询

Linq to sql 等效LINQ到SQL查询,linq-to-sql,Linq To Sql,有人能帮我完成下面SQL的等效LINQ查询吗? 我是LINQ的新手 Select * From Students_History SH Where SH.Active = 1 and SH.ModifiedAt in ( select MAX(SH1.ModifiedAt)from Students_History SH1 group by SH1.StudentId) 这就是我尝试过的 var q = from h in Students_History where h.A

有人能帮我完成下面SQL的等效LINQ查询吗? 我是LINQ的新手

Select * From Students_History SH
Where SH.Active = 1 and SH.ModifiedAt in (
select MAX(SH1.ModifiedAt)from Students_History SH1
group by SH1.StudentId)
这就是我尝试过的

var q = 
    from h in Students_History
    where h.Active=1
    group h by h.StudentId into g
    select new 
    {
        StudentID = g.Key,
        LatestModified = g.Max (x => x.ModifiedAt)
    }
这个linq查询没有给我正确的结果,不知何故,active=1被忽略了


我的Students\u History表中大约有十几个字段,我想要所有这些字段,而不仅仅是studentId和ModifiedAt。

您需要使用
==
操作符进行比较。

尝试以下操作:

var q =
    from hg in Students_History
    group hg by hg.StudentId into g
    join h in Students_History on g.Key equals h.StudentId
    where h.Active == 1 && h.ModifiedAt == g.Max(x => x.ModifiedAt)
    select new
    {
        StudentID = h.StudentId,
        LatestModified = h.ModifiedAt
    }

活跃油井=1不是唯一的问题。我也在寻找正确的问题。