Can I get a combination that translates the following into a LINQ statement.
SELECT reports.* FROM [dbo].[ReportLists] rl INNER JOIN [dbo].[ReportItems] ri ON rl.Id = ri.ReportListId INNER JOIN [dbo].[Reports] reports ON ri.Id = reports.ReportItemId WHERE reports.createdDate IN ( SELECT MAX(report_max_dates.createdDate) FROM [dbo].[Reports] report_max_dates GROUP BY report_max_dates.seedLot )
I am currently dealing with this:
db.ReportLists.Select(rl => db.ReportItems .Where(ri => ri.ReportListId == rl.Id) .Select(ri => db.Reports .Where(r => r.ReportItemId == ri.Id) .GroupBy(r => new { r.seedLot }) .Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault())));
The problem with LINQ above is that it returns records with names. Inside the database, I save the history of all records (hence the need for the first order to be created in the created order. When I change the report from heading x to heading y, it appears under both headings when I need only the most recent record, which will therefore be under the heading y.
EDIT Sorry for the lack of details. I have a report table that contains information about the report (the seed is an identifier). Whenever a report is edited, a new record is inserted (or the old one is updated), so the history is saved. In this case, the maximum record for createdDate indicates that the report is the most recent record to be displayed. Then the reports are grouped into headers or ReportItems. These report elements contain a title and related reports. These report elements are stored in a ReportList so that I can print the JSON in the required format and simply contain the status column and identifier associated with ReportItems.
If the report is transferred from heading a to heading b, a new record will be entered with the foreign key of the heading associated with the name it was changed to. When this happens, the above LINQ returns a record under each individual ReportItem when it should only return the newest record for heading b (from the above example). In addition, the LINQ statement returns only the most recent record for the created file.
Here are my class structures (which also mimic a DB structure)
public class ReportList { public int Id {get;set;} public string status {get;set;} public List<ReportItem> {get;set;} } public class ReportItem { public int Id {get;set} public string title {get;set;} public List<Report> {get;set;} } public class Report { public int Id {get;set;} public string Lot {get;set;} ... Other data ... public DateTime createdDate {get;set;} }
Thanks, Dman
source share