By user9879650


2019-10-09 14:17:25 8 Comments

I have a list of integer and I want to check whether it contains several values, but it does not work and always return true in if statement. The code is here:

if(IntegerList.Contains(1 | 2 |3 | 4))
{   //do something    }

1 comments

@pappbence96 2019-10-09 14:27:14

The way you're doing it checks whether the list contains 1 | 2 | 3 | 4 which is equal to 7 (bitwise OR). If your IntegerList does contain the value 7 then your query will indeed return true.

If you want to check whether IntegerList contains ALL of the items in SearchList, you can use the following LINQ query:

bool containsAll = SearchList.All(x => IntegerList.Contains(x));

If you want to know whether IntegerList contains ANY of the items in SearchList, just swap All with Any, like so:

bool containsAny = SearchList.Any(x => IntegerList.Contains(x));

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Why not inherit from List<T>?

27 Answered Questions

[SOLVED] Get int value from enum in C#

  • 2009-06-03 06:46:39
  • jim
  • 1537514 View
  • 1820 Score
  • 27 Answer
  • Tags:   c# enums casting int

4 Answered Questions

[SOLVED] Is there a reason for C#'s reuse of the variable in a foreach?

24 Answered Questions

[SOLVED] Convert a string to an enum in C#

  • 2008-08-19 12:51:55
  • Ben Mills
  • 705564 View
  • 892 Score
  • 24 Answer
  • Tags:   c# string enums

12 Answered Questions

[SOLVED] Pass Method as Parameter using C#

28 Answered Questions

[SOLVED] How do I get the path of the assembly the code is in?

25 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

10 Answered Questions

[SOLVED] IEnumerable vs List - What to Use? How do they work?

19 Answered Questions

[SOLVED] Proper use of the IDisposable interface

28 Answered Questions

[SOLVED] When to use struct?

  • 2009-02-06 17:37:55
  • Alex Baranosky
  • 271758 View
  • 1389 Score
  • 28 Answer
  • Tags:   c# struct

Sponsored Content