I had been using the method in Jon Skeet's answer, but another one occurred to me using Concat
. The Concat
method performed slightly better in a limited test, but it's a hassle and I'll probably just stick with Contains
, or maybe I'll write a helper method to do this for me. Either way, here's another option if anyone is interested:
// Given an array of id's
var ids = new Guid[] { ... };
// and a DataContext
var dc = new MyDataContext();
// start the queryable
var query = (
from thing in dc.Things
where thing.Id == ids[ 0 ]
select thing
);
// then, for each other id
for( var i = 1; i < ids.Count(); i++ ) {
// select that thing and concat to queryable
query.Concat(
from thing in dc.Things
where thing.Id == ids[ i ]
select thing
);
}
This was not remotely scientific. I imagine your database structure and the number of IDs involved in the list would have a significant impact.
I set up a test where I did 100 trials each of Concat
and Contains
where each trial involved selecting 25 rows specified by a randomized list of primary keys. I've run this about a dozen times, and most times the Concat
method comes out 5 - 10% faster, although one time the Contains
method won by just a smidgen.