What Does Include() Do in Linq

What does Include() do in LINQ?

Let's say for instance you want to get a list of all your customers:

var customers = context.Customers.ToList();

And let's assume that each Customer object has a reference to its set of Orders, and that each Order has references to LineItems which may also reference a Product.

As you can see, selecting a top-level object with many related entities could result in a query that needs to pull in data from many sources. As a performance measure, Include() allows you to indicate which related entities should be read from the database as part of the same query.

Using the same example, this might bring in all of the related order headers, but none of the other records:

var customersWithOrderDetail = context.Customers.Include("Orders").ToList();

As a final point since you asked for SQL, the first statement without Include() could generate a simple statement:

SELECT * FROM Customers;

The final statement which calls Include("Orders") may look like this:

SELECT *
FROM Customers JOIN Orders ON Customers.Id = Orders.CustomerId;

.Include() .Where() in Linq to Entities query

I will try something, I hope I got your idea clearly though I am not sure I did.

so I guess your question is this

I need to get Patients treated by Doctors using some Product working
in Territories. The relationship exists

and here is how i would do it.

var ans = ctx
.PatientMap
.Include(p => p.Doctor)
.Include(p => p.Product)
.Include(p => p.Institution)
.Include(p => p.Doctor.TerritoryDoctorPanel
.Where(p =>
// some doctors, doctorIDs is list of all doctors id you want in case you are using id retrieval
doctorIDs.Contains(p.DoctorID) &&
//working in some territory
//similar to this, you can filter any doctor Attribute
p.Doctor.TerritoryDoctorPanel.Any(t => /*Add condition for TerritoryDoctorPanel here */) &&
(p.IDProduct == null || p.IDProduct == IDProduct.Value) &&
(p.EndOfTreatment == null) &&
// Product Promotion line conditions
// also similar to this you can filter any product attribute
(p.Product.PromotionalLine.Any(pl => /*Add condition for promotional lines here*/)))
.ToList()

LINQ using Contain with Include

This should work:

var payments = _dataContext.Payments
.Include(payments => payments.Mandate)
.ThenInclude(mandates => mandates.Product)
.ThenInclude(products => products.Company)
.Where(p => companyIds.Contains(p.Mandate.Product.Company.PlatformCompanyId))

Include() in LINQ to Entities query

The navigation property name in your Task class is Status. So, you would have to use:

var tasks = from tsk in dbcontext.TaskSet.Include("Status")
select tsk;

But since you are working with the DbContext API a better option is to use the type-safe overload of Include:

using System.Data.Entity;
// You must add a using statement for this namespace to have the following
// lambda version of Include available

//...

var tasks = from tsk in dbcontext.TaskSet.Include(t => t.Status)
select tsk;

You will get Intellisense and compile-time checks which helps to avoid issues with wrong strings like you had.

C# linq include before-after where

Ordering of instructions like you've shown often won't make a difference in EF or LINQ to SQL. The query builder turns your entire LINQ statement into an abstract logical representation, and then another pass converts the logical structure into a SQL statement. So the WHERE predicates are all going to end up in the same place. The predicates inside a First() just get pushed over to the WHERE clause. The Include statements also get accumulated and projected to JOINs to include the extra columns needed to produce the included entity.

So the short answer is that EF will usually produce the most logical SQL statement regardless of the order in which you constructed your LINQ statement. If you need to tune it further, you should look at a stored procedure where you can hand-craft the SQL.

How does LINQ expression syntax work with Include() for eager loading

I figured it out, thanks for the suggestions anyway.
The solution is to do this (2nd attempt in my question):

var qry = (from a in Actions
join u in Users on a.UserId equals u.UserId
select a).Include("User")

The reason intellisense didn't show Include after the query was because I needed the following using:

using System.Data.Entity;

Everything worked fine doing this.

What is Entity Framework Include() and when to use it?

In your case I assume "Customers" and "Invoices" are two separate tables.

Let's say you got one customer :

var customer = context.Customers.FirstOrDefault();

If you try to reach the invoices for that customer :

customer.Invoices

What will happens? It will depends your ORM configuration. There are 2 modes.

Lazy loading
With that mode, which is the default one I think, you will have a first call to the database to retrieve the customer (but only the customer) then a second call to the database to retrieve the invoices for that customer. But that second call will only happens when you will try to reach the invoices.

Eager loading
If you don't want to have multiple database calls and you want to get all data in one call you can disable the lazy loading. But if you get the customer the same way I showed above you will not get the invoices (you will get "null" instead).
The "Include" allow EF to know you want to retrieve the customer AND the invoices in one database call:

var customer = context.Customers.Includes(c => c.Invoices).FirstOrDefault();

And when you will try to get the invoices for that customer, everything will be already in memory so no additional database call.



Related Topics



Leave a reply



Submit