views:

48

answers:

2

How can I use Linq-to-sql to a search like this:

where obj.id equals any of the following {1,2,3,4}

I would guess I could use the in or perhaps contains?

where obj.id in Enumerable.Range( (int) myEnum.Start, (int) myEnum.End) ) ?

+4  A: 

You can use .Contains(), like this:

var list = new List<int> { 1, 2, 3, 5 };
var result = from s in DB.Something
             where list.Contains(s.Id)
             select s;

This will get translated to a parameterized form of:

WHERE Id IN (1, 2, 3, 5)
Nick Craver
A: 
var myCustomers = new short[] {1,2,3,4};
var foo = db.Customers.Where(c=> myCustomers.Contains(c.ID));
p.campbell
@Johannes: careful not to paste this sample code into your production app!
p.campbell