开发者

Equivalent LINQ to SQL query

Can anyone help me with the equiva开发者_如何学运维lent LINQ query for the SQL below? I am new to 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)

This is what I have tried

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)
    }

This linq query does not give me the right result and somehow the active=1 is ignored

I have about dozen fields in my Students_History table and I want all those fields not just studentId and ModifiedAt.


Try this:

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
    }


You need to compare using the == operator.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜