Reputation: 2168
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.
EDIT
How would I model the statement now If I had a separate title which contains multiple reports. For instance I have reports l,m,n,x,y,z. reports l m n have title "hello" linked to them through a foreign key reportList_Id, and x y c have title "goodbye" linked in the same manner.
Basically I now need to get all the reports into the following object
public class ReportRoot : DbContext {
public DbSet<ReportList> reportList {get;set;}
}
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;}
public DateTime createdDate {get;set;}
}
There is the full listing of my classes. What I need to do is get back the reportList which contains multiple reportItems (which are lists of reports with titles). So these ReportItems will contain the reports themselves. So i need to get back all the reports with max createdDate as we did with the first part of the query but I need to get them back as ReportList objects which contain the ReportItems such that the multiple reports have their titles.
I need the objects in the above format for proper serialization and deserialization of JSON into my objects.
I came up with this, which separates them and gives back titles, but I have unwanted records being returned such as a record that I changed the title for is showing up under both titles.
db.ReportLists.Select(rl => db.ReportItems
.Where(ri => ri.ReportListId == rl.Id)
.Select(ri => db.Reports
.Where(r => r.ReportItemId == ri.Id)
.GroupBy(r => r.seedLot)
.Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault())))
Thansk, Dman
Upvotes: 2
Views: 368
Reputation: 60493
I may have bad understood, but basically, you want the last created date for each distinct Lot ? If yes, then
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault());
or to do like you (but I'm not sure it's the easiest way)
var maxDates = Reports.GroupBy(r => r.Lot)
.Select(x => x.Max(g => g.createdDate).ToList();
var result = db.Reports.Where (m => maxDates.Contains(m.createdDate));
EDIT
Your example is not that clear (I change a little bit the names for clarity).
With code first, you should have something like that (idea is the same)
a class Report
public class Report {
public int Id {get;set;}
public int Lot {get;set;}
public Datetime CreatedDate {get;set;}
public virtual Category Category {get;set;} //Navigation property
}
a class Category
public class Category {
public int Id {get;set;}
public string Title {get;set;}
public virtual IList<Report> ReportList {get;set;} //Navigation property
}
and eventually a "result class"
public class ReportList {
public string Title {get;set;}
public List<Report> ReportList {get;set;}
}
Then query could become
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x =x.createdDate).FirstOrDefault())
.GroupBy(m => m.Category.Id)
.Select(g => new ReportList {
Title = g.FirstOrDefault().Category.Title,
ReportList = g.OrderBy(x => x.Lot)
});
Upvotes: 1
Reputation: 14086
var recentDates = Reports
.GroupBy(r=>r.SeedLot, r=>r.CreatedDate)
.Select(rg=>rg.Max());
var result =
from r in Reports
join d in recentDates
on r.createdDate equals d
select r;
Upvotes: 2