0

I have two tables which are customers and payments.

Table Customers ==> Columns [CustomerId, ...other customer info].
Table Payments ==> Columns [PaymentId, CustomerId, Year, ...other payment info]

How can I get customers that haven't paid this year. I have no idea what I need.

import System.Linq;
var result = from customers in context.Customers 
join payments from context.Payments 
on customers.CustomerId equals payments.CustomerId
where payments.Year == 2021 into paymentsCount
where paymentsCount.count == 0;
 
Ann L.
  • 13,078
  • 5
  • 34
  • 63
Emre
  • 27
  • 3

1 Answers1

2

If you have proper navigation properties:

var query = context.Customers
    .Where(c => !c.Payments.Any(p => p.Year == 2021));

If there is no proper navigation property:

var query = context.Customers
    .Where(c => !context.Payments.Any(p => p.CustomerId == c.CustomerId && p.Year == 2021));
Svyatoslav Danyliv
  • 13,476
  • 1
  • 9
  • 23