0

I have an list and I want to compare a dynamic integer with the elements of the array. How should I do that ??

I have a list as findcolnumber whose elements are: 1,8,9,10,11 and they are generated dynamically. I generate one another integer rowcount. If the value of rowcount is equalto any of those single value 1,8,9,10,11, then only it should go in the for loop.

The list findcolnumber is generated dynamically. Also findcolnumber[what should i keep inside this bracket].

if(findcolnumber[] == rowcount) {
    proceed
}
3
  • I think that you must use findcolnumber.Count Commented Mar 14, 2013 at 19:41
  • I want to make sure I understand the question. Do you want to know how to check if an integer exists in an array of integers? Commented Mar 14, 2013 at 19:47
  • It is a list. and yes i want to compare the list with an integer. Commented Mar 14, 2013 at 19:51

1 Answer 1

2

try this:

you will have to include this namespace

using System.Linq;

and then you can do this:

if(findcolnumber.Contains(rowcount))
{
    //your logic
}

where rowcount is some integer i.e.

int rowcount = getDynamicIntegar();

and findcolnumber is:

int[] findcolnumber = {1,8,9,10,11};

linq .Contains returns boolean value, if your dynamic integer will exist in the integral array, .Contains will return true otherwise false

.Contains extension, if you bother to see, will be available for your List<int> as well.

same way you can compare any List or Collection implementing IEnumerable interface

you can only pass the collection's base datatype in .Contains

i.e. if findcolnumber is List<int> then

you can findcolnumber.Contains(integralValueOrVariable)

if findcolnumber is List<string> then

you can findcolnumber.Contains(stringValueOrVariable)

Sign up to request clarification or add additional context in comments.

10 Comments

you will have to include this namespace. No you have not to.
It is a list. How should i proceed then ? findcolumn is a list.
I will still not use extension methods for simpler lists and arrays.
@user2163048 both my method and Manish's method work on Lists, infact any datastructure(including a simple array) that implements the IEnumerable<T> interface.
AgentFire, you have to include that namespace, i am pretty sure, and Ankiet, iterating through a loop is a bit longer code thn directly doing .Contains
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.