AlanRubinoff
AlanRubinoff

Reputation: 385

Get max date with linq where condition, inside query

I have the following two tables that store the calls that the users made,

Users
-----
IdUser int PK,
Name varchar(50)

Calls
------
idCall int PK,
idUser int FK,
CallDate datetime

This is the code i tried but i cant seem to get it to work

var resultado = (from Users in ctx.Users
                 join Calls in cts.Calls on Users.idUser equals Calls.idUser
                 select new
                 {                           
                     Name= Users.Name,
                     MaxDate=(from c in Calls select c.CallDate).Where(u=>u.IdUser==Users.IdUser).Max()  
                 }).ToList();

Example: I have

User:
-----
0 John Doe

Calls:
------
0 0 23/11/2015
0 0 25/11/2015
0 0 26/11/2015

The query should return

Alan 26/11/2015

Upvotes: 0

Views: 791

Answers (1)

John Boker
John Boker

Reputation: 83699

Not tested, but does something like this work?

var resultado = (from Users in ctx.Users                 
             select new
             {                           
                 Name= Users.Name,
                 MaxDate=(from c in ctx.Calls where c.IdUser == Users.IdUser select c.CallDate).Max()  
             }).ToList();

Upvotes: 1

Related Questions