dimanche 28 juin 2015

What is the Max function equivalent of SQL in LINQ?

I have this SQL query that I need to convert to LINQ:

SELECT * FROM USER U
INNER JOIN (
    SELECT USERID , MAX(SALESDATE) AS MAXDATE
    FROM SALES
    GROUP BY USERID
) S ON U.ID = S.USERID

so far, this is what I've come up with LINQ:

var Users = (from d in db.Users
             join s in db.Sales on d.Id equals s.UserId
             select new Models.User
             {
                Id = d.Id,
                UserName = d.UserName,
                FirstName = d.FirstName,
                LastName = d.LastName,
                EmailAddress = d.EmailAddress,
                PhoneNumber = d.PhoneNumber,
                LastPurchase = s.SalesDate
             }).Max(x => x.SalesDate);

However, I'm pretty sure the result is not the same. Can anyone suggest the correct way of converting this to LINQ? I'm still new to learning LINQ. I really appreciate the help.

Aucun commentaire:

Enregistrer un commentaire