views:

10

answers:

1

Using the following database table structure:

Order Table:
OrderId
OrderName

OrderItem Table:
OrderId
ItemId

Item Table:
ItemId
ItemName

I have an 'Order' entity that has an 'Items' collection. What I need to do is return all orders that contain certain items. Example: All orders with items with id: 1, 4 and 5 (I don't care if it has other items as well). I currently have the selected id's in a List object (it doesn't have to be a list though). I have tried various ideas with 'contains' method but to no avail.

Because OrderItem is a link table, I can't access it as an entity itself. If I could then presumably I could do:

context.OrderItems.Where(m => selectedIds.Contains(m.ItemId))

or something like that.

Is there a way of doing this in a single query without having to loop around the results?

Thanks

A: 
context.Orders.Where(o => o.Items.Any(i => selectedIds.Contains(i.ItemId)))

This requires EF 4.

Craig Stuntz
Thanks again Craig
Nick Reeve