Many for many choose LINQ for objects

I have two tables Serviceand Provider. Between them is a join table ServiceProviderthat has only 2 fields - one for each of the two PKs. When added to edmx, the join of the many-to-many table is abstracted and cannot be seen (as expected).

This is all right, if only I want to get providers based on this service. From this question :

it looks like the answer will be simple:

var query = from p in entities.Providers
            from s in entities.Services
            where s.Id == 15
            select p;

but it returns ALL suppliers. What am I doing wrong here?

+5
source share
4 answers
var query = entities.Providers.FirstOrDefault(p => p.Id == 15).Services.ToList();
+4
source

Try the following:

var res = from s in entities.Services
          where s.Id == 15
          select s.Provider;

EDIT

. .

+2

Isn't it as easy as

var matchingProviders = entities.Services.Single(s=>s.Id==15).Providers;
+2
source

You can try using join, for example:

entity.Providers.Join(entity.Services, c => c.ID, p => p.ID,(c, p) => new { Providers= c, Services= p })
-1
source

All Articles