score:220
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;
score:2
think of it as enforcing eager-loading in a scenario where your sub-items would otherwise be lazy-loading.
the query ef is sending to the database will yield a larger result at first, but on access no follow-up queries will be made when accessing the included items.
on the other hand, without it, ef would execute separte queries later, when you first access the sub-items.
score:31
i just wanted to add that "include" is part of eager loading. it is described in entity framework 6 tutorial by microsoft. here is the link: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
excerpt from the linked page:
here are several ways that the entity framework can load related data into the navigation properties of an entity:
lazy loading. when the entity is first read, related data isn't retrieved. however, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. this results in multiple queries sent to the database — one for the entity itself and one each time that related data for the entity must be retrieved. the dbcontext class enables lazy loading by default.
eager loading. when the entity is read, related data is retrieved along with it. this typically results in a single join query that retrieves all of the data that's needed. you specify eager loading by using the
include
method.explicit loading. this is similar to lazy loading, except that you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. you load related data manually by getting the object state manager entry for an entity and calling the collection.load method for collections or the reference.load method for properties that hold a single entity. (in the following example, if you wanted to load the administrator navigation property, you'd replace
collection(x => x.courses)
withreference(x => x.administrator)
.) typically you'd use explicit loading only when you've turned lazy loading off.because they don't immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.
Source: stackoverflow.com
Related Query
- What does LINQ return when the results are empty
- What is LINQ and what does it do?
- What does this C# code with an "arrow" mean and how is it called?
- Does this LINQ code perform multiple lookups on the original data?
- What statements does Linq to entities support?
- What does => mean in a Linq Expression
- What does Any() mean in this LINQ query?
- How does linq actually execute the code to retrieve data from the data source?
- Does the order of the include and the where matter in a LINQ query?
- LINQ Source Code Available
- How does this linq code that splits a sequence work?
- What does this LINQ query do?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- LINQ - What does (x, i) do?
- How does coding with LINQ work? What happens behind the scenes?
- At what point is a LINQ data source determined?
- What does the code query.Take(() => 1) do?
- creating Linq to sqlite dbml from DbLinq source code
- Does LINQ convert code to SQL queries
- What kind of object does a linq statement return?
- LINQ What does 'select new' do?
- How do I know what does EF translate Linq queries into?
- at what point does linq-to-sql or linq send a request to the database
- why does this linq code get exponentially slower when applying First() to projection?
- LINQ equals - what does it call under the covers?
- what does this .net line of code means
- what does a linq query return when no results are found
- VB.Net Linq with datatables - select from one table what does not exist in the other
- what does "non-filtering clauses" mean when running linq on parse.com?
- LINQ - trying to use Include, custom Function and return custom type. Include does not work
More Query from same tag
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
- Check if item exists in list and push value into it — in one shot
- Converting LINQ (OrderBy/ThenBy) Code to CompareTo
- Linq to hibernate common function
- Get count of a datatable column contains with specific value
- Using LINQ in cross-join query of a datatable in C#
- Sorting with OrderBy, ThenBy
- Linq FirstOrDefault List inside a query
- can my code improve from using LINQ?
- Why does XElement not behave like a reference type?
- Linq select match list from other list performance
- Extension method on custom collection throwing error
- Fastest way to check whether a single element in common exists between two enumerables
- Linq query has a bug?
- LINQ navigation property multiple sources
- create new object within C# Linq syntax on Select
- Order datagridview descending with linq
- Converting Lambda Functions into Lambda Expression
- Databinding LINQ query results (with join tables) to DataGrid
- LINQ from x in Table_name?
- LINQ compare 2 tables, only return previous item, not all
- Trying to Filter XML using XPath
- LINQ: Get sum from a list grouped by an inside list
- Stripping characters from PredicateBuilder to improve search accuracy
- Why is this linq expression not working?
- How to Reverse All Strings in String Array using LINQ?
- Deserialize Web Api OData response
- Linq Table select where IN IQueryable
- Select X with Max(F(X))
- Sorting dynamic list not working