LINQ to SQL Tutorial
Lección 28 de 38 · Part 9 Eager loading in LINQ to SQL · Bases de datos
Part 9 Eager loading in LINQ to SQL
Text version of the video
http://csharp-video-tutorials.blogspot.com/2014/09/part-9-eager-loading-in-linq-to-sql.html
Slides
http://csharp-video-tutorials.blogspot.com/2014/09/part-9-eager-loading-in-linq-to-sql_11.html
LINQ to SQL Tutorial – All Text Articles & Slides
http://csharp-video-tutorials.blogspot.com/2014/09/linq-to-sql-tutorial.html
LINQ to SQL Tutorial Playlist
Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
In this video we will discuss eager loading in LINQ to SQL. This is continuation to Part 8. Please watch Part 8 before proceeding.
What is Eager loading
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query.
In LINQ to SQL there are 2 ways we can eager load data
1. Using DataLoadOptions
2. Using Projection
Using DataLoadOptions to eager load related entities in LINQ to SQL :
using (SampleDataContext dbContext = new SampleDataContext())
{
dbContext.Log = Console.Out;
// Load related Employee entities along with the Department entity
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith[Department](d =] d.Employees);
dbContext.LoadOptions = loadOptions;
foreach (Department dept in dbContext.Departments)
{
Console.WriteLine(dept.Name);
foreach (Employee emp in dept.Employees)
{
Console.WriteLine(“t” + emp.FirstName + ” ” + emp.LastName);
}
}
}
DataLoadOptions is present in System.Data.Linq namespace
Run the application, and notice that there is only one query which retrieves all the departments and their related employee entities.
Using Projection to eager load related entities in LINQ to SQL :
using (SampleDataContext dbContext = new SampleDataContext())
{
dbContext.Log = Console.Out;
var linqQuery = from dept in dbContext.Departments
select new { Name = dept.Name, Employees = dept.Employees };
foreach (var dept in linqQuery)
{
Console.WriteLine(dept.Name);
foreach (Employee emp in dept.Employees)
{
Console.WriteLine(“t” + emp.FirstName + ” ” + emp.LastName);
}
}
}
Again, run the application, and notice that there is only one query which retrieves all the departments and their related employee entities.
Now let’s do the same thing with the web application example we worked with in Part 8.
Using DataLoadOptions to eager load related entities in LINQ to SQL :
using (SampleDataContext dbContext = new SampleDataContext())
{
dbContext.Log = Response.Output;
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith[Department](d =] d.Employees);
dbContext.LoadOptions = loadOptions;
gvDepartments.DataSource = dbContext.Departments;
gvDepartments.DataBind();
}
Using Projection to eager load related entities in LINQ to SQL :
using (SampleDataContext dbContext = new SampleDataContext())
{
dbContext.Log = Response.Output;
gvDepartments.DataSource = from dept in dbContext.Departments
select new { Name = dept.Name, Employees = dept.Employees };
gvDepartments.DataBind();
}