Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variable in a set of values

0 views
Skip to first unread message

hon123456

unread,
Mar 24, 2010, 4:40:32 AM3/24/10
to
Dear all,

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.

Patrice

unread,
Mar 24, 2010, 4:56:40 AM3/24/10
to
Hi,

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...

Ovidiu Fragă

unread,
Mar 24, 2010, 5:13:03 AM3/24/10
to
SELECT *
FROM
  angajati
WHERE
  angajati.nume_angajat IN
           (SELECT
                  angajati.nume_angajat
            FROM
                  angajati
            WHERE
                   nume_angajat LIKE 'boc%')


where angajati is table name, and nume_angajat is the field name.
you can pass this to a TableAdapter's SelectCommand.

Patrice

unread,
Mar 24, 2010, 5:19:18 AM3/24/10
to
As you see without some context you'll find that different peoples interpret
differently your question...

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...

hon123456

unread,
Mar 24, 2010, 10:07:24 AM3/24/10
to
On 3月24日, 下午5時19分, "Patrice" <http://scribe-en.blogspot.com/> wrote:
> As you see without some context you'll find that different peoples interpret
> differently your question...
>
> 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.5...@TK2MSFTNGP06.phx.gbl...

>
>
>
> > Hi,
>
> > 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" <peterhon...@yahoo.com.hk> a écrit dans le message de groupe
> > de discussion :
> > 1aa8bbcf-8254-4222-af7d-294b3ae52...@d30g2000prn.googlegroups.com...

> >> Dear all,
>
> >>         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.- 隱藏被引用文字 -
>
> - 顯示被引用文字 -

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.

Jeff Johnson

unread,
Mar 24, 2010, 12:09:25 PM3/24/10
to
"hon123456" <peter...@yahoo.com.hk> wrote in message
news:1aa8bbcf-8254-4222...@d30g2000prn.googlegroups.com...

> 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.


J.B. Moreno

unread,
Mar 24, 2010, 12:28:50 PM3/24/10
to
hon123456 <peter...@yahoo.com.hk> wrote:

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

Patrice

unread,
Mar 24, 2010, 12:48:28 PM3/24/10
to
> 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.

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


Jeff Johnson

unread,
Mar 24, 2010, 1:43:43 PM3/24/10
to
"J.B. Moreno" <pl...@newsreaders.com> wrote in message
news:240320100828505325%pl...@newsreaders.com...

>> 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.


0 new messages