JL.
JL.

Reputation: 81292

equivalent for the SQL IN operator?

Given the following query:

Select * from myTable where stringField in ('A','B','C')

I'll be pulling items from a db, and haven't yet decided if I will store them in a list, or an array, or some other collection object, but ideally want something built in (no custom class), and without manual iteration.

Focus is on performance and code maintainability.

What would be the most efficient way to do this in .net?

Thanks in advance

Upvotes: 0

Views: 68

Answers (3)

Jon
Jon

Reputation: 437386

The most convenient way would be Enumerable.Contains (.NET 3.5 upwards). It should also be performing well, although I have not benchmarked.

Example:

var list = new[] { "A", "B", "C" };
var matches = myTable.Where(o => list.Contains(o.stringField));

Upvotes: 2

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

You might be able to use a Linq Intersect with a custom Equality Comparer

http://msdn.microsoft.com/en-us/library/bb302032

Upvotes: 0

Adrian Iftode
Adrian Iftode

Reputation: 15663

var searchItems = new [] {"A", "B", "C"};

var query = from c in myTable
            where searchItems.Contains(c.stringField)
            select c;

This if it is about LINQ 2 SQL.

Upvotes: 1

Related Questions