Monday, June 29, 2009

Immediate Loading Relational Data in LINQ to SQL

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