How can I convert this SQL query to a linq expression?
Select distinct Clients.Id
From Clients
Join Orders On Clients.Id = Orders.ClientsId and Orders.Date < DATEADD(DD, -Clients.Term, CAST(GETDATE() AS DATE))
Yes, I know about the Database.SqlQuery<>() function. Probably it's the best solution in terms of performance. But code uniformity is preferred, so I need to use linq.
Here is my solution:
var outdatedClients = context.Clients.GroupJoin(context.Orders,
client => client.Id,
order => order.ClientId,
(client, order) => new
{
Id = client.Id,
Term = client.Term,
Orders = order.Any(order => DbFunctions.DiffDays(order.Date, _datenow) > client.Term)
})
.ToList()
.Where(client => client.Orders == true);
But maybe it`s possible to do it more elegantly?