0

I have this set:

List<short> delegatingUserRoles = await (from urols in _dbContext.UsersRoles
                                                        join u in _dbContext.Users on urols.UserId equals u.UserId
                                                        where (u.Username == delegation.UserDelegating)
                                                        select urols.RoleId).Distinct().ToListAsync();
            
            if (delegatingUserRoles.ConvertAll(i => (int)i).All(item => (delegation.UserSelectedRoles).Contains(item))){
                //true
            }

delegation.UserSelectedRoles is a list of int, subset of delegatingUserRoles

example delegatingUserRoles has values 1,2,3,4 while delegation.UserSelectedRoles has values 1,2,3

Why it does not enter the if?

  • 2
    Can you provide the real code? That code doesn't compile if `delegatingUserRoles` and `UserSelectedRoles` are `List` because a `List` is not `T`. Maybe you use an `ArrayList` ort `List`, but then you should really show us your code. – Tim Schmelter Apr 17 '23 at 10:19
  • Unless `delegatingUserRoles` is a `List`, that code won't return anything because it won't even compile – CodeCaster Apr 17 '23 at 10:21
  • 1
    Are you only interested in a set-wise comparison? What would you expect the result to be if `delegatingUserRoles` was (say) {1, 1, 3, 2, 4} and `userSelectedRoles` was {1, 2, 3, 4}? The order is different *and* `delegatingUserRoles` has 1 twice. Note that if you want to treat these as sets, then it would be best to *create* sets... then you can use the existing methods for that. – Jon Skeet Apr 17 '23 at 10:24
  • Using the `Except()` method, you can remove elements that are common in both lists. After doing this, you can compare the number of elements in the original list and say contains if there is a difference. `bool contains=delegatingUserRoles.Except(UserSelectedRoles).Count() != list1.Count; Console.WriteLine(contains);` – MrAlbino Apr 17 '23 at 10:24
  • 2
    You could use [`HashSet.IsSubsetOf`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.issubsetof?view=net-7.0): `bool contained = UserSelectedRoles.ToHashSet().IsSubsetOf(delegatingUserRoles)`. But you should consider to use `HashSet` in the first place(for both). – Tim Schmelter Apr 17 '23 at 10:27
  • Maybe you wanna use a hashset for this? – Son of Man Apr 17 '23 at 10:46
  • { 1, 2, 3 } does not contain 4... – CodeCaster Apr 17 '23 at 10:56

3 Answers3

2
var l1 = new List<Int32> { 1 };
var l2 = new List<Int32> { 1,2,3 };

var r = l1.All(l => l2.Contains(l));

r = true

Ready Cent
  • 1,821
  • 3
  • 18
  • 25
1

That Contains method tests whether a specific item is contained in a list. It doesn't test whether a list of items are all contained in a list. You could do this:

if (UserSelectedRoles.All(item => delegatingUserRoles.Contains(item))
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • 1
    `O(n*n)` though – CodeCaster Apr 17 '23 at 10:22
  • It's worth noting that that treats both collections as effectively sets - where the count of each item is irrelevant, as is the ordering. That may well be okay for the OP, but it's probably worth clarifying. – Jon Skeet Apr 17 '23 at 10:22
  • 1
    @CodeCaster, how long would the lists have to be for that to be worth worrying about? Longer than I suspect any list of user roles would ever be. – jmcilhinney Apr 17 '23 at 10:24
  • 1
    We write code for the general case, not the specific one. The next reader may have more than a handful of items in their lists. It never hurts to warn, and/or to explain what your code does. – CodeCaster Apr 17 '23 at 10:24
  • @JonSkeet, fair point in general but, as this question is dealing with lists of user roles, unlikely to be relevant here. – jmcilhinney Apr 17 '23 at 10:25
  • 2
    @jmcilhinney: If the OP were likely to be the only person ever reading the answer, I'd agree. But as we know Stack Overflow answers are often read as the result of searches, I'd suggest clarifying the answer to note its assumptions/limitations. – Jon Skeet Apr 17 '23 at 10:26
  • see code updates – user18363545 Apr 17 '23 at 10:51
0

Because the Contains() method checks if a single object is contained within the list, not if a list is contained within another list.

I think you need All() here.

AVTUNEY
  • 923
  • 6
  • 11