2

我在 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'

我在实体框架中有以下查询

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();

当我尝试编译它时,我得到

错误 3 名称 'p' 不在 'equals' 左侧的范围内。考虑交换 'equals' 两边的表达式。
错误 4 名称 'link' 不在 'equals' 右侧的范围内。考虑交换 'equals' 两边的表达式。

4

1 回答 1

9

正是错误在说什么

p.PhoneNumberId equals link.PhoneNumberId 

应该

link.PhoneNumberId equals p.PhoneNumberId

完整代码

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();
于 2012-07-18T09:10:57.677 回答