I have a table in my database representing releases of software; the relevant columns of the schema being as follows:
ProductID int
PlatformID tinyint
Date datetime
All are not null, and there is also a unique key assigned to the first two columns.
I would like to build a query (using LINQ) which returns the latest release (e.g. Date) for each platform (PlatformID), but no more than one record per platform (PlatformID), and is sorted by Date descending.
How can I do this?
EDIT: Here is the solution I used:
var product = ...;
var latestReleases = product.Releases
.GroupBy(p => p.PlatformID)
.Select(group => group.OrderByDescending(p => p.Date).FirstOrDefault())
.OrderByDescending(release => release.Date)
for clarity...
foreach (var release in latestReleases)
{
Console.WriteLine("The latest release of MyProduct for {0} is {1}.",
release.PlatformID, // of course this would be a string in real code ;)
release.Version);
}