EntityFramework - contains query of composite key

前端 未结 7 1626
渐次进展
渐次进展 2020-11-22 06:06

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 sa

7条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 06:44

    In the absence of a general solution, I think there are two things to consider:

    1. Avoid multi-column primary keys (will make unit testing easier too).
    2. But if you have to, chances are that one of them will reduce the query result size to O(n) where n is the size of the ideal query result. From here, its Solution 5 from Gerd Arnold above.

    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.

提交回复
热议问题