65.9K
CodeProject is changing. Read more.
Home

Immediate Loading Relational Data in LINQ to SQL

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.40/5 (3 votes)

Jan 20, 2010

CPOL
viewsIcon

9508

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