Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

Immediate Loading Relational Data in LINQ to SQL

3.40/5 (3 votes)
20 Jan 2010CPOL 9.5K  
In LINQ to SQL the relational data is loading only when we refer that data, other terms its lazy loading of data.But we can load relational data ,suppose we have an Employee Table and also having an Employee details table related to Employee Table, we can load that relational data using...
In LINQ to SQL the relational data is loading only when we refer that data, other terms its lazy loading of data.But we can load relational data ,suppose we have an Employee Table and also having an Employee details table related to Employee Table, we can load that relational data using DataLoadOptions while querying the context.

DataLoadOptions options = new DataLoadOptions();
options.AssociateWith<Employee>(p => p.EmployeeDetails.
Where(q => q.IsActive == false));
options.LoadWith<Employee>(p => p.EmployeeDetails);
Context.LoadOptions = options;
var emp = from p in DataContext.Employee
         where p.EmployeeID == employeeID &&
         p.IsDeleted == false
         select p

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)