Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I write LINQ to SQL to select rows where an ID is in an array of integers of unknown size?

Tags:

linq-to-sql

Say I want to pass an array of integers into a method that runs some LINQ to SQL, how would I say something like "where SuchID is in array[] select"?

like image 899
MetaGuru Avatar asked Mar 14 '11 14:03

MetaGuru


1 Answers

Use Contains.

int[] ids = // populate ids
var query = from e in db.SomeTable
            where ids.Contains(e.SuchID)
            select e;

LINQ to SQL will translate this to a WHERE clause using IN.

like image 179
jason Avatar answered Sep 29 '22 21:09

jason