Converting SQL statement into Linq

Posted by DMan on Stack Overflow See other posts from Stack Overflow or by DMan
Published on 2012-09-06T15:23:23Z Indexed on 2012/09/06 15:38 UTC
Read the original article Hit count: 346

Filed under:
|
|

I'm trying to convert the following to a LINQ to SQL statement in C#. Can anyone give me a hand? Basically my table keeps record of all history of changes such that the created date max date for each seedlot is the most recent record and the correct one to show.

SELECT 
    reports.* 
FROM
    [dbo].[Reports] reports
WHERE
    reports.createdDate
IN (
    SELECT 
        MAX(report_max_dates.createdDate) 
    FROM 
        [dbo].[Reports] report_max_dates
    GROUP BY 
        report_max_dates.Lot
    )

So far this is what I have.

var result = (from report in db.Reports
    where report.createdDate == (from report_max in db.Reports
                                group report_max by report_max.Lot into report_max_grouped
                                select report_max_grouped).Max()
    select report);

I can't figure out how to get the MAX dates for all reports and how to do an IN statement on the report.createdDate.

Thansk, Dman

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql