given a list of ids, I can query all relevant rows by:
context.Table.Where(q => listOfIds.Contains(q.Id));
But how do you achieve the same functionality when the Table has a composite key?
given a list of ids, I can query all relevant rows by:
context.Table.Where(q => listOfIds.Contains(q.Id));
But how do you achieve the same functionality when the Table has a composite key?
I tried this solution and it worked with me and the output query was perfect without any parameters
This is a nasty problem for which I don't know any elegant solution.
Suppose you have these key combinations, and you only want to select the marked ones (*).
How to do this is a way that Entity Framework is happy? Let's look at some possible solutions and see if they're any good.
Solution 1:
Join
(orContains
) with pairsThe best solution would be to create a list of the pairs you want, for instance Tuples, (
List<Tuple<int,int>>
) and join the database data with this list:In LINQ to objects this would be perfect, but, too bad, EF will throw an exception like
which is a rather clumsy way to tell you that it can't translate this statement into SQL, because
Tuples
is not a list of primitive values (likeint
orstring
).1. For the same reason a similar statement usingContains
(or any other LINQ statement) would fail.Solution 2: In-memory
Of course we could turn the problem into simple LINQ to objects like so:
Needless to say that this is not a good solution.
db.Table
could contain millions of records.Solution 3: Two
Contains
statementsSo let's offer EF two lists of primitive values,
[1,2]
forId1
and[2,3]
forId2
. We don't want to use join (see side note), so let's useContains
:But now the results also contains entity
{1,3}
! Well, of course, this entity perfectly matches the two predicates. But let's keep in mind that we're getting closer. In stead of pulling millions of entities into memory, we now only get four of them.Solution 4: One
Contains
with computed valuesSolution 3 failed because the two separate
Contains
statements don't only filter the combinations of their values. What if we create a list of combinations first and try to match these combinations? We know from solution 1 that this list should contain primitive values. For instance:and the LINQ statement:
There are some problems with this approach. First, you'll see that this also returns entity
{1,6}
. The combination function (a*b) does not produce values that uniquely identify a pair in the database. Now we could create a list of strings like["Id1=1,Id2=2","Id1=2,Id2=3]"
and do(This would work in EF6, not in earlier versions).
This is getting pretty messy. But a more important problem is that this solution is not sargable, which means: it bypasses any database indexes on
Id1
andId2
that could have been used otherwise. This will perform very very poorly.Solution 5: Best of 2 and 3
So the only viable solution I can think of is a combination of
Contains
and ajoin
in memory: First do the contains statement as in solution 3. Remember, it got us very close to what we wanted. Then refine the query result by joining the result as an in-memory list:It's not elegant, messy all the same maybe, but so far it's the only scalable2 solution to this problem I found, and applied in my own code.
Solution 6: Build a query with OR clauses
Using a Predicate builder like Linqkit or alternatives, you can build a query that contains an OR clause for each element in the list of combinations. This could be a viable option for really short lists. With a couple of hundreds of elements, the query will start performing very poorly. So I don't consider this a good solution unless you can be 100% sure that there will always be a small number of elements. One elaboration of this option can be found here.
1As a funny side note, EF does create a SQL statement when you join a primitive list, like so
But the generated SQL is, well, absurd. A real-life example where
MyIntegers
contains only 5(!) integers looks like this:There are n-1
UNION
s. Of course that's not scalable at all.Later addition:
Somewhere along the road to EF version 6.1.3 this has been greatly improved. The
UNION
s have become simpler and they are no longer nested. Previously the query would give up with less than 50 elements in the local sequence (SQL exception: Some part of your SQL statement is nested too deeply.) The non-nestedUNION
allow local sequences up to a couple of thousands(!) of elements. It's still slow though with "many" elements.2As far as the
Contains
statement is scalable: Scalable Contains method for LINQ against a SQL backendin Case of composite key you can use another idlist and add a condition for that in your code
or you can use one another trick create a list of your keys by adding them
In the absence of a general solution, I think there are two things to consider:
For example, the problem leading me to this question was querying order lines, where the key is order id + order line number + order type, and the source had the order type being implicit. That is, the order type was a constant, order ID would reduce the query set to order lines of relevant orders, and there would usually be 5 or less of these per order.
To rephrase: If you have a composite key, changes are that one of them have very few duplicates. Apply Solution 5 from above with that.
You need a set of objects representing the keys you want to query.
If you have two lists and you simply check that each value appears in their respective list then you are getting the cartesian product of the lists - which is likely not what you want. Instead you need to query the specific combinations required
I'm not completely sure that this is valid use of Entity Framework; you may have issues with sending the
Key
type to the database. If that happens then you can be creative:You can create an isomorphic function (prime numbers are good for this), something like a hashcode, which you can use to compare the pair of values. As long as the multiplicative factors are co-prime this pattern will be isomorphic (one-to-one) - i.e. the result of
p1*Id1 + p2*Id2
will uniquely identify the values ofId1
andId2
as long as the prime numbers are correctly chosen.But then you end up in a situation where you're implementing complex concepts and someone is going to have to support this. Probably better to write a stored procedure which takes the valid key objects.
You can create a collection of strings with both keys like this (I am assuming that your keys are int type):
Then you can just use "Contains" on your db: