LINQ Include C#

 

LINQ (Language Integrated Query) is a Microsoft .NET Core & .NET Framework component that adds native data querying capabilities to .NET languages, originally released as a major part of .NET Framework 3.5 in 2007.

LINQ Include provides you the ability to include related entities to be loaded from the database. Follow our tutorial to understand how to use this Linq functionality to  load related entities automatically

What is LINQ Include?

There are several ways to read data from the data source and load results into the navigation properties of an entity. Eager Loading provides the functionality of the LINQ Include() by retrieving the related data whenever the entity is read. This will produce a large result set at first but no more queries will be send to the database to fetch the related data. On the contrary, multiple queries will be called to retrieve the related data without the Include() function. The other two options are lazy loading and explicit loading which do not retrieve the related data in a single join query.

If the requirement is to retrieve the related data for every entity, eager loading is the best option to use in such scenario because a single call is made to the database instead of sending multiple commands to fetch the data by using Include() function. The Include() function might result in a complex join which will be interpreted by the SQL.

Lambda Expressions with LINQ Include:

In earlier versions of Entity Framework, you need to pass the navigation property name as an argument in the quotation marks to the LINQ Include() function. The compiler was not able to identify the compilation errors until the application is in running mode and executes the programming command. The second drawback is you cannot use Intellisense and refactoring feature.

The EF 4.1 brings the provision of eager loading using lambda with LINQ Include() function by using the EntityFramework.dll and System.Data.Entity namespaces.


using (var context = new DepartmentContext())
{
    // Load all departments and related students.
    var departmentList = context.Department
                            .Include(b => b.Students)
                            .ToList();
    // Load all Departments and related Students by using a plain string to specify the relationship
    var departmentsListB = context.Departments
                            .Include("Students")
                            .ToList();
}
 

LINQ Include vs Join:

Include() function retains the original structure and objects of the entities whereas the Join function gives the flat representation of the data. They both will acquire the same results but the representation is different for each function. The following code illustrates the programming difference between the Include and Where functions:


using (var context = new DepartmentContext())
{
    /// Load all departments and related students by using Include function
    var departmentList = context.Department
                            .Include(b => b.Students)
                            .ToList();
    /// Load all departments and related students by using Join function
    var departmentList = from d in context.Department
                            join s in context.Students on d.DepartmentId equals s.StudentId 
                            select new {d, s};
}

 

LINQ Include vs Where:

Include is an alternative to the Where function in the LINQ. The following code illustrates the programming difference between the Include and Where functions:


using (var context = new DepartmentContext())
{
    // Load one department “Computer Science” and its related students by using Include function
    var department = context.Departments
                            .Where(b => b.Name == "Computer Science")
                            .Include(b => b.Students)
                            .FirstOrDefault();
    // Load one department “Computer Science” and its related Students by using Where function
    var departmentB = from d in context.Department
                            where d.Name == "Computer Science")
                            select d;
}

 

LINQ Include vs Select:

Include and Select provides the functionality of retrieving a list or a single object. The following code illustrates the difference between the Include and Select functions:


using (var context = new DepartmentContext())
{
    // Load all departments and its related students by using Include function and return a list with same UniversityName “ABC University”
    var departmentList = context.Departments
                                .Include(b => b.Students) 
                                .Select(x => new { Department = x, UniversityName = "ABC University" }) 
                                .ToList();
    // Load all departments and its related students by using Select function and return a list with same UniversityName “ABC University”
    var departmentListB = from d in context.Department
                                where d.Name == "Computer Science")
                                select d;
}

 

LINQ Include vs SelectMany:

Include and SelectMany provide retrieving data at multiple levels. The following code illustrates the difference between the Include and SelectMany function:


using (var context = new DepartmentContext())
{
    // Load all departments and courses by using Include function 
    var departmentList = context.Departments
                                .Include(b => b.Students.Select(it => it.Courses))
                                .ToList();
    // Load all departments and courses by using SelectMany function 
    var departmentListB = context.Departments
                                 .Where(d =>d.Name == "Computer Science")
                                 .SelectMany(d => d.Courses);
}

Summary:

LINQ Include provides you the ability to include related entities to be loaded from the database. Follow our tutorial to understand how to use this Linq functionality to  load related entities automatically.