I have an array of in memory objects that represent the composite keys that I want to query by eg:
public class Key
{
public string Part1 {get;set;}
public string Part2 {get;set;}
}
Now say I have an IQueryable<Table1>
and I want to return all the records with the above Key
s. Table1 does not have a surrogate key, it has a composite key made up of 2 columns. I also want to avoid per row retrieval.
How can I do this? I tried standard join, but for obvious reasons my ORM is confused when in memory objects is thrown in the mix.
In similar cases I've found the best solution to be one in which a first selection is done in the database, followed by the exact matches in memory:
If you've got Keys
then
dbSelection
will also contain{1,1
} and{2,2}
(but not the vast majority of other records). These are filtered out by the second query.The advantage is that the database query can take advantage of indexes, which is not possible if you work with computed keys (like concatenated key values).
The downside is that you must ensure that
parts1
andparts2
can't grow excessively, otherwise the SQLIN
statements will become highly inefficient or even crash because of too many elements (we're talking many thousands of items here for Sql Server). But this is true for any solution usingContains
.