variable in a set of values

H

hon123456

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

Patrice

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" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
 
O

Ovidiu Fragă

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

Patrice

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# ?
 
H

hon123456

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 : [email protected]...







- 顯示被引用文字 -

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

Jeff Johnson

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

J.B. Moreno

hon123456 said:
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.

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

Patrice

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
 
J

Jeff Johnson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top