代码之家  ›  专栏  ›  技术社区  ›  K.Z

LINQ any compare to Guid List throw错误

  •  0
  • K.Z  · 技术社区  · 7 年前

    我正在处理LINQ查询,在该查询中,我选择了针对role的所有声明角色可能有声明,也可能没有声明,所以我离开了join,它工作正常,如下对象claimList所示现在我需要指定状态对象true或false的基础上,如果存在一个角色,我试图使用任何,但它扔错误

    错误

     cannot convert from system.Guid to system.Func<system.Guid, bool>
    

    语言级集成查询

    var o2 = (from role in Context.Roles.Where(x=>x.Id == Guid.Parse("22000010-0002-4ADD-BC6F-7556616t66656"))
                      join roleClaimRef in Context.RoleClaims on role.Id equals roleClaimRef.RoleId into rc
                      select new
                      {
                         role,
                         roleClaim = rc.DefaultIfEmpty(),
                         claimList = (from claim in Context.Claims
                                      select new
                                      {
                                          claim,
                                          status = rc.Select(x=>x.ClaimId).Any(claim.Id) // throw error here....
                                      })
                      }).ToList();
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Gerald Chifanzwa    7 年前

    你说什么

    status = rc.Select(x=>x.ClaimId).Any(claim.Id) ,将Guid作为参数传递给Linq函数Any,该函数需要返回布尔值的函数( See documentation from the Microsoft Docs )

    我想你想做的是从 rc 其中 ClaimId 财产等于你的 claim.Id .

    在这种情况下,请执行以下操作:

    status = rc.Select(x=>x.ClaimId).Any(claimId => claimId == claim.Id)

    希望这有帮助。

        2
  •  1
  •   Hasan Gholamali    7 年前

    你的问题是因为 Guid.Parse 在linq中,必须创建一个变量并分配它,然后使用如下变量:

    Guid tmpId = Guid.Parse("22000010-0002-4ADD-BC6F-7556616t66656");
    var o2 = (from role in Context.Roles.Where(x=>x.Id == tmpId)
                  join roleClaimRef in Context.RoleClaims on role.Id equals roleClaimRef.RoleId into rc
                  select new
                  {
                     role,
                     roleClaim = rc.DefaultIfEmpty(),
                     claimList = (from claim in Context.Claims
                                  select new
                                  {
                                      claim,
                                      status = rc.Select(x=>x.ClaimId).Any(claim.Id)
                                  })
                  }).ToList();
    
        3
  •  1
  •   Kiran Joshi    7 年前

    检查您的 ID 使用 where 条件

       var o2 = (from role in Context.Roles.Where(x=>x.Id == Guid.Parse("22000010-0002-4ADD-BC6F-7556616t66656"))
                          join roleClaimRef in Context.RoleClaims on role.Id equals roleClaimRef.RoleId into rc
                          select new
                          {
                             role,
                             roleClaim = rc.DefaultIfEmpty(),
                             claimList = (from claim in Context.Claims
                                          select new
                                          {
                                              claim,
                                              status = rc.Where(a=>a.ClaimId==claim.Id).Select(x=>x.ClaimId)//check if claim id exist or not
                                          })
                          }).ToList();