I have two tables Service
and Provider
. Between them is a joining table ServiceProvider
which has only 2 fields - one for each of the two PKs. When added to the edmx the joining many-to-many table is abstracted away and can't be seen (as expected).
This is all fine except when I want to get Providers based on a given service. From this question:
it looks like the answer would be simply:
var query = from p in entities.Providers
from s in entities.Services
where s.Id == 15
select p;
but this returns ALL providers. What am I doing wrong here?
var query = entities.Providers.FirstOrDefault(p => p.Id == 15).Services.ToList();
Isn't it as simple as
var matchingProviders = entities.Services.Single(s=>s.Id==15).Providers;
Try this:
var res = from s in entities.Services
where s.Id == 15
select s.Provider;
EDIT
Corrected and tested the query on real-life model and data. It works now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With