Easiest way to query relational data

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
KW
Posts: 135
Joined: Tue 19 Feb 2008 19:12

Easiest way to query relational data

Post by KW » Fri 05 Feb 2010 22:28

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.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Mon 08 Feb 2010 12:14

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;

KW
Posts: 135
Joined: Tue 19 Feb 2008 19:12

Thank you

Post by KW » Thu 11 Feb 2010 23:31

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.

Post Reply