C# LINq查询引发异常
我正在运行以下查询C# LINq查询引发异常,c#,linq,llblgenpro,C#,Linq,Llblgenpro,我正在运行以下查询 var allroles = from l in metaData.Role select l.RoleId; var personroles = from k in metaData.PersonRole where k.PersonId == new Guid(Session["user_id"].ToString()) select k.RoleId; Dictionary<Guid, str
var allroles = from l in metaData.Role select l.RoleId;
var personroles = from k in metaData.PersonRole
where k.PersonId == new Guid(Session["user_id"].ToString())
select k.RoleId;
Dictionary<Guid, string> allroleswithnames =
(from l in metaData.Role
select new { l.RoleId, l.Description })
.ToDictionary(u => u.RoleId, u => u.Description);
var avl_roles = from j in allroles.Except(personroles)
select new
{
RoleId = j,
Description = allroleswithnames[new Guid(j.ToString())]
};
clist_avl_roles.DataSource = avl_roles;
clist_avl_roles.DataBind();
var allroles=from元数据中的l.Role选择l.RoleId;
var personroles=来自metaData.PersonRole中的k
其中k.PersonId==新Guid(会话[“用户id”].ToString())
选择k.RoleId;
字典allroleswithnames=
(来自metaData.Role中的l)
选择新的{l.RoleId,l.Description})
.ToDictionary(u=>u.RoleId,u=>u.Description);
var avl_roles=来自所有角色中的j。除了(personroles)
选择新的
{
RoleId=j,
Description=allroleswithnames[新Guid(j.ToString())]
};
clist_avl_roles.DataSource=avl_roles;
clist_avl_roles.DataBind();
avl_角色代码处的代码抛出错误
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
子查询返回了多个值。当子查询
如下=,!=,=或者当子查询用作表达式时。
事实上,roleid有多行具有相同的个人id。如何重写查询以处理这种情况
var personId = new Guid(Session["user_id"].ToString());
var personRoles = metaData.PersonRole
.Where(pr => pr.PersonId == personId)
.Select(pr => pr.RoleId);
var avl_roles = from r in metaData.Role
where !personRoles.Contains(r.RoleId)
select new { r.RoleId, r.Description };
或者在单个查询中
var avl_roles = from r in metaData.Role
join pr in metaData.PersonRole.Where(x => x.PersonId == personId)
on r.RoleId equals pr.RoleId into g
where !g.Any()
select new { r.RoleId, r.Description };