K.Z
K.Z

Reputation: 5075

LINQ any compare to Guid List throw error

I am working on LINQ query in which I selecting all the claims against role. The role may or may not have claim so I did left join which is working fine as in following object claimList. Now I need to assign status object true or false based on if claim exist for a role which I am trying to use any but it throw error

error

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

LINQ

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

Upvotes: 0

Views: 55

Answers (3)

Kiran Joshi
Kiran Joshi

Reputation: 1876

Check your ID using where condition

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

Upvotes: 1

Gerald Chifanzwa
Gerald Chifanzwa

Reputation: 1347

Where you say

status = rc.Select(x=>x.ClaimId).Any(claim.Id), you are passing Guid as a parameter into the Linq function Any, which requires a function that returns a boolean (See documentation from the Microsoft Docs).

I assume what you intend to do is select from rc where the ClaimId property is equal to your claim.Id.

In that case, do this:

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

Hope this helps.

Upvotes: 1

Hasan Gholamali
Hasan Gholamali

Reputation: 633

Your problem is because of using Guid.Parse in linq, you must create a variable and assign it, after that use the variable like below:

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

Upvotes: 1

Related Questions