Multiple Join in Entity Framework

c# entity-framework sql sql-server

Question

I have the following query in TSQL

select * from users 
inner join linkUserPhoneNumber on users.UserId = linkUserPhoneNumber.UserId
INNER JOIN PhoneNumber ON PhoneNumber.PhoneNumberId =
    linkUserPhoneNumber.PhoneNumberId
    where UserName = 'superuser' and password ='password'

I have the following query in Entity Framework

var query = (from u in myEntities.Users
  join link in myEntities.linkUserPhoneNumbers on u.UserId equals link.UserId 
  join p in myEntities.PhoneNumbers on p.PhoneNumberId equals link.PhoneNumberId
  where u.UserName == Username && u.Password == Password
  select u).ToList();

When I try to compile it, I get

Error 3 The name 'p' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.
Error 4 The name 'link' is not in scope on the right side of 'equals'. Consider swapping the expressions on either side of 'equals'.

1
2
1/4/2013 3:43:08 PM

Accepted Answer

Exactly what the error is saying

p.PhoneNumberId equals link.PhoneNumberId 

should be

link.PhoneNumberId equals p.PhoneNumberId

Full code

var query = (from u in myEntities.Users
  join link in myEntities.linkUserPhoneNumbers on u.UserId equals link.UserId 
  join p in myEntities.PhoneNumbers on link.PhoneNumberId equals p.PhoneNumberId
  where u.UserName == Username && u.Password == Password
  select u).ToList();
6
1/4/2013 3:44:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow