0

次のクエリを実行しています

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

エラーをスローするavl_rolesのコードのコード

Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

実際には、同じ個人IDを持つroleidには複数の行があります。この状況を処理するためにクエリを書き直すにはどうすればよいですか?

4

1 に答える 1

1
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 };
于 2012-12-28T09:53:17.027 に答える