Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the role of the Include method

I am trying to learn LINQ for a project. But I am a little confused by the Include method. What is it used for? What is the difference between the following two initializations of the album variable?

var album = storeDB.Albums.Include("Artist").ToList();
var album = storeDB.Albums.ToList();
like image 671
ankit0311 Avatar asked Nov 18 '25 12:11

ankit0311


1 Answers

Include is an extension method which is used by EF to enable Eager Loading of your Entities.

In the first case, by specifying Include("Artist"), when you retrieve Album entities from your Albums set, you are instructing LINQ to also retrieve the associated Artist entity (usually associations are through foreign keys in the database, although you can associate in your model as well).

In the second case, you aren't pulling through any related entities when Albums are fetched.

Note that as of Entity Framework 4.1 and onwards, there is a preferred mechanism for using Include which takes a lambda, so the string associations become strongly typed, i.e.:

var album = storeDB.Albums.Include(alb => alb.Artist).ToList();

Just remember to import System.Data.Entity when using the lambda extension.

like image 140
StuartLC Avatar answered Nov 20 '25 02:11

StuartLC