Search code examples
vb.netlinq-to-sql

Linq to SQL matching claim type and list of claim values


I'm trying to run a query that matches users having a claim that matches a list of values (as well as limiting by a username).

Using vb.NET.

The statement that I have is:

Dim AllUsers = (From u In Users 
                From c In u.Claims
                Where (u.IsTemplate = False And u.Name.ToLower.Contains(q.ToLower)) _
                And (c.ClaimType = CDpermission _
                And CDPermissionValues.Contains(c.ClaimValue)) Select u).Distinct.ToList()

CDpermission is a string, CDPermissionValues is an array of string values.

The statement appears to completely ignore the part with the claims - i.e. it's just matching the username.

If I use one of the specific claim values then the statement runs as intended, i.e.:

Dim AllUsers = (From u In Users
                From c In u.Claims
                Where (u.IsTemplate = False And u.Name.ToLower.Contains(q.ToLower)) _
                And (c.ClaimType = CDpermission _
                And c.ClaimValue = "INSTIGATE") Select u).Distinct.ToList()

CDPermissionValues may contain one or more values, so I need to check against a list.

Any help gratefully received!


Solution

  • I would think that this would work:

    Dim AllUsers = (From u In Users
                    Where Not u.IsTemplate And
                          u.Name.ToLower.Contains(q.ToLower) And
                          u.Claims.Any(Function(c) c.ClaimType = CDpermission And
                                                   CDPermissionValues.Contains(c.ClaimValue))).ToList()