Reputation: 23
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
}
Upvotes: 0
Views: 101
Reputation: 12375
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)
Upvotes: 2