-1

I have a Key object that has an attribute of type List<int> and it is called KeyProperties. Key object also has an int Id attribute.

class Key
{
   public int Id;
   public List<int> KeyProperties;
}

I have a list of integers called currentSelection.

I would like to prevent the user from creating a Key that has the exact same integers as in currentSelection. That means if currentSelection is:

List<int> = new List<int>() {7,8,10};

I do not want the user to create a Key with a KeyProperties attribute of (7,8,10). But still the user will be able to create Keys with (7,8) or (7,8,15).

How can I achieve this using LINQ?

disasterkid
  • 6,948
  • 25
  • 94
  • 179

3 Answers3

1
//currentSelection.Sort();
//theKey.KeyProperties.Sort();
bool valid = !currentSelection.SequenceEqual(theKey.KeyProperties)

I recommend to switch to a ISet`1 implementation like HashSet`1 though to skip the sorting process if the order is irrelevant.

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
1

Use All:

List<int> currSelection = new List<int>() {7,8,10};

public bool CanCreateKey(List<int> keyToCheck)
{
   return !currSelection.All(i => keyToCheck.Contains(i));
}
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
0
bool valid = !currentSelection.OrderBy(x => x).SequenceEqual(theKey.KeyProperties.OrderBy(x => x));
Mourndark
  • 2,526
  • 5
  • 28
  • 52