Page 1 of 1

Easiest way to query relational data

Posted: Fri 05 Feb 2010 22:28
by KW
Given a many to many relationship ( one customer has many Reservations and a Reservation can belong to more than one customer)

Customer Reservation

Whats the easiest way to query Customers and their corresponding Reservations and vica versa ( Using linq to entities)


There is a great example of a single hop:

var query = from it in context.products.Include("ProductCategories")
orderby it.productcategories.CategoryName, it.ProductName
select it;


I want to be able to navigate from one entity to another no matter how far the relation is.

Posted: Mon 08 Feb 2010 12:14
by AndreyR
You can use Include like in the following code sample:

Code: Select all

from t1s in db.T1.Include("T2").Include("T2.T1") select t1s;

Thank you

Posted: Thu 11 Feb 2010 23:31
by KW
AndreyR wrote:You can use Include like in the following code sample:

Code: Select all

from t1s in db.T1.Include("T2").Include("T2.T1") select t1s;
That works. And for anyone else interested, Include key word is eager loading, so it will immediately load those into the framework.