I am new to LINQ to SQL, hence this question.
By default if I do a query for a row, does LINQ to SQL fetch all the foriegn key rows?
eg:
let us say I have 1->N relationship will 3 tables
Branch -N> Departments -N> Employees
If I query a brand row
Branch b = (
from b in brachDataContext.Branches
where b.id = id
select b).ToList();
Does this fetch all the departments related to the branch row and the employees related to these departments?
If so, wont that be a huge object. each branch might have 10 departments and each department might have 1000 employees, and we might not need all the records.
Linq 2 SQL uses deferred execution by default, which means that your Departments will be fetched if you enumerate over that collection.
However, you can control whether they should be included in the initial query by setting the
context.LoadOptionsproperty like soCheck this post out for more info on deferred execution.
And also read up on the difference between IEnumerable and IQuerable and their behaviors. It is the key to understanding all Linq providers.
As a heads up, my experience is that Linq 2 Sql works for smaller projects and rapid development etc. If you have anything else in mind, consider moving to Entity Framework or NHibernate for a more feature complete experience.