In SQL, there is a condition IN . e.g. products in (a,b,c). I
want to know whether there exist a same statement in C#. Or maybe I
should use other syntax? Thanks.
We'll likely need some details as SQL is not C#. In particular this is the
other way round. Most collections have a method that allows to find out if
a value is found in the collection.
Try for example :
http://msdn.microsoft.com/en-us/library/bb384015.aspx
You could create an array that contains the a,b, c value and then test if
this array contains the products value...
--
Patrice
"hon123456" <peter...@yahoo.com.hk> a écrit dans le message de groupe de
discussion :
1aa8bbcf-8254-4222...@d30g2000prn.googlegroups.com...
Do you try to pass some SQL from C# or do you want just to test if a value
is part of a list in C# ?
--
Patrice
"Patrice" <http://scribe-en.blogspot.com/> a écrit dans le message de groupe
de discussion : eIaRw$yyKHA...@TK2MSFTNGP06.phx.gbl...
Dear all,
I am not asking for SQL, I know SQL syntax. But what I want
is how to write in C# that is to find a value which is
in a collection or array or list. for example, does c# have Var_A in
('aaa','bbb','ccc') . do it by collection or array?
Any example web page? Thanks.
> In SQL, there is a condition IN . e.g. products in (a,b,c). I
> want to know whether there exist a same statement in C#. Or maybe I
> should use other syntax? Thanks.
No, there is no direct C# equivalent to SQL's IN operator. You either have
to use multiple || operators or you need to process the possibilities in a
loop.
Most collection classes such as arrays and list have a method named
"Contains". So you declare your array myarray and then see if it has
what your looking for using myarray.Contains(whatever).
You can also use LINQ to do the same thing.
--
J.B. Moreno
So I confirm my previous post. You'll find a method called "Contains" on
most if not all lists. See for example :
http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx
--
Patrice
>> In SQL, there is a condition IN . e.g. products in (a,b,c). I
>> want to know whether there exist a same statement in C#. Or maybe I
>> should use other syntax? Thanks.
>
> Most collection classes such as arrays and list have a method named
> "Contains". So you declare your array myarray and then see if it has
> what your looking for using myarray.Contains(whatever).
>
> You can also use LINQ to do the same thing.
Actually, the only reason arrays appear to have a Contains() method is due
to System.Linq being referenced. Comment that out of your code and all those
extension methods disappear. Just wanted to make it clear that arrays do not
"natively" have a Contains() method.