English 中文(简体)
Equivalent LINQ tokou query
原标题:Equivalent LINQ to SQL query

Can anyone help me with the equivalent 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)

这是我所尝试的。

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

这里的争 que没有给我带来正确的结果,有些情况却忽视了积极标准。

我有大约十几个领域: 历史表和我希望所有这些领域不仅仅是学生。 Id and ModedAt.

最佳回答

为此:

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
    }
问题回答

您需要使用=>运营商进行比较。





相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签