86

What would be the query for:

select s.* from Service s 
inner join ServiceAssignment sa on sa.ServiceId = s.Id
where  sa.LocationId = 1

in entity framework?

This is what I wrote:

 var serv = (from s in db.Services
                join sl in Location on s.id equals sl.id
                where sl.id = s.id
                select s).ToList();

but it's wrong. Can some one guide me to the path?

3
  • Look there : stackoverflow.com/questions/37324/… Commented Apr 15, 2013 at 21:58
  • 1
    You are using assignment = instead of comparison == in where clause. Also you don't need that if you already joined on that fields. Commented Apr 15, 2013 at 22:06
  • 3
    Isn't where sl.id = s.id redundant with your join condition? Commented Oct 6, 2015 at 22:08

3 Answers 3

110
from s in db.Services
join sa in db.ServiceAssignments on s.Id equals sa.ServiceId
where sa.LocationId == 1
select s

Where db is your DbContext. Generated query will look like (sample for EF6):

SELECT [Extent1].[Id] AS [Id]
       -- other fields from Services table
FROM [dbo].[Services] AS [Extent1]
INNER JOIN [dbo].[ServiceAssignments] AS [Extent2]
    ON [Extent1].[Id] = [Extent2].[ServiceId]
WHERE [Extent2].[LocationId] = 1
Sign up to request clarification or add additional context in comments.

5 Comments

Isn't that a left outer join?
@JonathanWood No, that is inner join. Left outer join will be generated by group join.
Could we use from s in db.Services join sa in db.ServiceAssignments on s.Id equals sa.ServiceId || s.Id equals null where sa.LocationId == 1 select s
Many thanks... I've used List<Item> items = (..your code..).toList();
@JoSmo if you have a navigation property (from example below) you can use Method syntax no problem. But if not, it would look like this (pretty hairy): db.Services.Join(db.ServiceAssignments, s=>s.Id, sa=>sa.ServiceId, (s,sa)=> new{service = s, alert=sa}).Where(ssa=>ssa.alert.LocationId ==1).Select(ssa=>ssa.service);
76

In case anyone's interested in the Method syntax, if you have a navigation property, it's way easy:

db.Services.Where(s=>s.ServiceAssignment.LocationId == 1);

If you don't, unless there's some Join() override I'm unaware of, I think it looks pretty gnarly (and I'm a Method syntax purist):

db.Services.Join(db.ServiceAssignments, 
     s => s.Id,
     sa => sa.ServiceId, 
     (s, sa) => new {service = s, asgnmt = sa})
.Where(ssa => ssa.asgnmt.LocationId == 1)
.Select(ssa => ssa.service);

3 Comments

This wouldn't work if the FK to ServiceAssignment happened to be nullable, would it?
@Cardin in that case I would suggest the C#6 ?. operator. If you don't have that, just check for null before using the navigation property. Generally, you don't add a bunch of defensive code in examples, so as not to confuse the primary point. If the FK is nullable, it would look like this: (C#6) db.Services.Where(s=>s?.ServiceAssignment.LocationId == 1); or like this in C#5: db.Services.Where(s=>s.ServiceAssignment != null && s.ServiceAssignment.LocationId == 1);
@MichaelBlackburnThat's true! It is more understandable. Thanks for the clarification. :)
8

You could use a navigation property if its available. It produces an inner join in the SQL.

from s in db.Services
where s.ServiceAssignment.LocationId == 1
select s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.