2

Consider two tables Bill and Product with a many to many relationship. How do you get all the bills for a particular product using Entity Sql?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Deepak N
  • 2,561
  • 2
  • 30
  • 44

2 Answers2

3

Something like this

SELECT B FROM [Container].Products as P
OUTER APPLY P.Bills AS B
WHERE P.ProductID == 1

will produce a row for each Bill

Another option is something like this:

SELECT P, (SELECT B FROM P.Bills)
FROM [Container].Products AS P
WHERE P.ProductID == 1

Which will produce a row for each matching Product (in this case just one) and the second column in the row will include a nested result set containing the bills for that product.

Hope this helps

Alex

Alex James
  • 20,874
  • 3
  • 50
  • 49
  • Alex, I just wanted to thank you. OUTER APPLY was the key that I was looking for to solve a different Entity SQL problem related to a left outer join on a many to many join (navigation property). Thank you again. – CkH Aug 31 '14 at 01:31
0

You need to use some linq like this;

...
using (YourEntities ye = new YourEntities())
{
   Product myProduct = ye.Product.First(p => p.ProductId = idParameter);
   var bills = myProduct.Bill.Load();       
}
...

This assumes that you have used the entitiy framework to build a model for you data. The bills variable will hold a collection of Bill objects that are related to your product object.

Hope it helps.

Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41