C# Lambda表达式不正确

C# Lambda表达式不正确,c#,linq,entity-framework,C#,Linq,Entity Framework,我从一个我认为构造正确的查询中得到了奇怪的结果 此查询返回2个值,这是正确的: int userId = GetUserId(); //Not exactly like this; simplified var context = new Entities(); //Get the roles var relations = (from q in context.UserHasRole where q.UserId == userId

我从一个我认为构造正确的查询中得到了奇怪的结果

此查询返回2个值,这是正确的:

int userId = GetUserId(); //Not exactly like this; simplified
var context = new Entities();
//Get the roles 
var relations = (from q in context.UserHasRole
                 where q.UserId == userId
                 select q).ToList();
List<Roles> roles = new List<Roles>();
foreach (var item in relations)
{
    Roles role = (from r in context.Roles
                  where r.Id == item.RoleId
                  select r).SingleOrDefault();
                  roles.Add(role);
 }
 return roles;

我应该如何构造lambda查询?

我以前没有这样做过,但我猜您应该这样做:

 //Get the roles 
 var roles = (from q in context.Roles
     where context.UserHasRole.Any(o => o.UserId == userId && o.RoleId == q.Id)
     select q).ToList();

 return roles;
尝试:


您要求的角色是“与UserHasRole表中的特定用户存在关系”和“与UserHasRole表中的角色存在关系”。第一个条件对每个角色都有相同的结果,因为它不依赖于角色

您希望角色“在用户和角色之间存在关系”


分解查询并查看您实际执行的操作:

from q in context.Roles
所以对于每个角色

where context.UserHasRole.Any(o => o.UserId == userId)
如果UserHasRole中存在具有给定用户ID的任何条目

&& context.UserHasRole.Any(p => p.RoleId == q.Id)
并且UserHasRole中存在一个roleId与当前角色匹配的条目

select q
选择该角色

我感觉这个逻辑不是你想要的,因为它正在独立地检查是否有一个带有userId的
UserHasRole
条目和一个带有角色Id的
UserHasRole
条目。但不是说有一个同时带有userId和角色Id的
UserHasRole
条目,这可能是你想要的效果:

//Get the roles 
var roles = (from q in context.Roles
             where context.UserHasRole.Any(o => o.UserId == userId
                                             && o.RoleId == q.Id)
             select q).ToList();

return roles;

如果不查看您的数据,我无法确定,但我希望您的查询将为您提供比您想要的更多的角色

以下是现有查询的PSEDOO代码:

Get all roles
From those roles, accept only those where these are true:
  Any mapping exists that matches the user ID
  Any mapping exists that matches the role ID
问题在于,您不能确保那些
任何
子句归结为相同的映射。用户不必具有特定的角色-用户只需具有某个角色,并且该角色必须映射到某个用户

以下是您真正希望查询执行的操作:

Get all roles
From those roles, accept only those where this is true:
  Any mapping exists that matches *both* the user ID and the role ID
要实现此查询,请执行以下操作:

var roles = (from r in context.Roles
             where context.UserHasRole.Any(uhr => uhr.UserId == userId
                 && uhr.RoleId == r.Id)
             select r)
            .ToList();

完美的我试过类似的方法,但没有成功。不过,这是我的拿手好戏。非常感谢你!没问题!我真的很喜欢lambda的表达方式。一直使用它们来清理列表,但还没有使用Linq。当我有机会的时候,我真的需要检查一下+1为我展示了一种分解问题的教学方法
Get all roles
From those roles, accept only those where these are true:
  Any mapping exists that matches the user ID
  Any mapping exists that matches the role ID
Get all roles
From those roles, accept only those where this is true:
  Any mapping exists that matches *both* the user ID and the role ID
var roles = (from r in context.Roles
             where context.UserHasRole.Any(uhr => uhr.UserId == userId
                 && uhr.RoleId == r.Id)
             select r)
            .ToList();