-1

How to check a single value is in a List. My List contains few random numbers: 13, 55, 34, 122, 322, 2132, 4345, 3000.

I want to have a bool check the list if it contains int 4 or any other number. List can have large numbers in three or four figures in the list.

It would be nice to guide me to design helper in webmatrix to provide bool results for this value check.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Nasir Raza
  • 11
  • 1

3 Answers3

1

Try this,intVariable is your variable to search

bool isInList = intList.IndexOf(intVariable) != -1;
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
0

You can't get away without iteration (loop). Use Enumerable.Any(that uses iterator internally) for simplicity.

var ints = new int[] {1,2,3,4,5};
var any5 = ints.Any( i=> i== 5);
Tilak
  • 30,108
  • 19
  • 83
  • 131
0

if the list is not that big why dont you loop inside the list

foreach(int a in MyList)
{
   if(a == "that_value")
   {
     break;
   }
}
befree2j
  • 361
  • 5
  • 11
  • I am already doing through loop; some what similar to this. Thanks. – Nasir Raza Jan 09 '13 at 14:00
  • I think there should be some other method to call this procedure through web helper in web matrix rather than putting this kind of loop over different places. – Nasir Raza Jan 09 '13 at 14:08