Learning from ScottGu's Linq Blog 👷🏼♂️👷🏼♀️
Blog 1
The Data Context
This class is the main conduit by which we query entities from the database and apply changes back to it.
Server Side Paging
NorthWindDataContext dc= new NorthWindDataContext();
var products = (from p in dc.Products
where p.Category.CategoryName.StartsWith("C")
select p).Skip(200).Take(10);
Skip(200) starting with row 200 Take(10) only take the first 10 rows
Blog 2
Entity Classes
Entity Classes map to tables within the database.
Entity classes do not have to derive from a specific base class - this means they can inherit from any object you want. All classes created using the Linq2SQL designer are partial classes - can optionally add additional properties, methods or events to them.
Renaming
Within the LINQ2SQL Designer surface you can rename entities, properties and associations.
Relaionships & Associations
The above association causes the following:-
- the Product entity has a "Cateogry" property
- the Category entty has a "Products" collection
Delayed/Lazy Loading
Can specify whether properties on entities are delay loaded (e.g. in the example above we are delay loading an image). This will only load the image when it is acutally being used, rather than in a simple query that is returning all the CategoryNames.
Stored Procedures
Can drag and drop SPROCs onto an entity in the LINQ2SQL designer surface where appropriate, for strong type linking (this isn't possible in all cases). For example, with a stroed proc that returns a list of Products, drag the proc onto the Products table to create a return type of IEnumerable\
Stored Procs for Updating/Inserting and Deleting
Can change the update/insert/delete methods on an entity to use a stored proc. This is done at the mapping layer of LINQ2SQL. This means that developers do not have to change their code even if an optimised sproc is later put in place.