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,760
  • 5
  • 35
  • 66
Emre
  • 67
  • 5

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
  • 21,911
  • 3
  • 16
  • 32