c# - Linq Entity Framework - get all customers that Ids are not in many to many table -


i have 2 tables:

customer

car

and many-to-many table mm, stores:

customer_id car_id

how customers car_id's not in many many table?

i tried this:

public async task<ienumerable<customermodel>> getnewcustomersforcar(int carid)         {             var sentcustomers = await _unit.repository<car>().queryable()                 .selectmany(a => a.aspnetusers, (b, a) => new { b, })                 .where(b => b.id == carid)                 .select(ba => new customermodel()                 {                     id = ba.a.id,                     email = ba.a.email                 })                 .tolistasync();              var allcustomers = await _unit.repository<aspnetuser>().queryable()                 .select(c => new customermodel()                 {                     id = c.id,                     email = c.email                 }).tolistasync();              return allcustomers.where(ac => !sentcustomers.contains(ac)); 

so select customers selected car, check customers, , in end select customers not contain id's many many customer table selected customer.

get customers didn't use car yet(all used car having id's selected car in many many table).

if have cars navigation property in aspnetuser entity, this:

var query= await _unit.repository<aspnetuser>()                       .queryable()                       .where(u=>!u.cars.any(c=>c.id==carid))                       .select(c => new customermodel()                                     {                                         id = c.id,                                         email = c.email                                     })                       .tolistasync(); 

also can change where .where(u=>u.cars.all(c=>c.id!=carid)),could more readable


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -