By negin motalebi


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

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

12 Answered Questions

[SOLVED] Pass Method as Parameter using C#

26 Answered Questions

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

28 Answered Questions

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

  • 2008-09-09 20:12:28
  • George Mauer
  • 576225 View
  • 737 Score
  • 28 Answer
  • Tags:   c# .net reflection

28 Answered Questions

[SOLVED] Get int value from enum in C#

  • 2009-06-03 06:46:39
  • jim
  • 1390591 View
  • 1668 Score
  • 28 Answer
  • Tags:   c# enums casting int

22 Answered Questions

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

  • 2008-08-19 12:51:55
  • Ben Mills
  • 609975 View
  • 781 Score
  • 22 Answer
  • Tags:   c# string enums

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
  • 256448 View
  • 1342 Score
  • 28 Answer
  • Tags:   c# struct

5 Answered Questions

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

Sponsored Content