I have a table in sql Server:
CREATE TABLE AD_Users (
[ImportID] BIGINT NOT NULL,
[LogonID] NVARCHAR (50) NOT NULL,
[EmployeeId] NVARCHAR (64) NULL,
[FirstName] NVARCHAR (64) NULL,
[LastName] NVARCHAR (64) NULL,
......
There are multiple versions of the data for each loginId, keyed by ImportID. So to get the latest info for each user I use:
SELECT LogonID,ImportID,EmployeeId, FirstName, LastName
FROM AD_Users adUsers where ImportID = (select Max(ImportID) from
AD_Users a where a.LogonID = adUsers.LogonId);
Now I want to do the same thing using Linq. EF maps this to a data set of:
public partial class ActiveDirectoryUser
{
public long ImportID { get; set; }
public string LogonId { get; set; }
public long EmployeeId{ get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
.......
So all I need to do is get the items from this dbset which have the max importId for that logonid.
So far I have:
var latests = this.DbSet.GroupBy(g => g.SystemUniqueUserID)
but I am at a loss where to go after that. Is it possible to do what I want in linq or should I just generate the SQL?
Not a duplicate of How do I get the MAX row with a GROUP BY in LINQ query? as this doesn't get the latest version of each item.