Reputation: 2533
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?
Upvotes: 5
Views: 4380
Reputation: 1478
You may try using join
, like so:
entity.Providers.Join(entity.Services, c => c.ID, p => p.ID,(c, p) => new { Providers= c, Services= p })
Upvotes: -1
Reputation: 7961
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.
Upvotes: 2
Reputation: 1128
var query = entities.Providers.FirstOrDefault(p => p.Id == 15).Services.ToList();
Upvotes: 4
Reputation: 50114
Isn't it as simple as
var matchingProviders = entities.Services.Single(s=>s.Id==15).Providers;
Upvotes: 2