
LINQ to SQL
1 Linq基础
1.1 Lamada Expession
Allow the definition of anonymous methods using more concise syntax.
delegate int del(int i);
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
public int myDelegateMethod(int i)
{
return i*i;
}
del myDelegateUsing = new del(myDelegateMethod);
int j = myDelegateUsing (5) //j=25
Func<Northwinds, IQueryable<Orders>, int> q =
CompiledQuery.Compile<Northwinds, int,IQueryable<Orders>>((Northwinds nw, int orderid) =>
from o in nw.Orders
where o.OrderId == orderid
select o );
Northwinds nw = new Northwinds(conn);
foreach (Orders o in q(nw, orderid))
{ ...}
1.2 Anonymous Types
An object initializer can also be used without specifying the class that will be created with the new operator. Doing that, a new class-an anonymous type- is created.
Customer c1 = new Customer { Name = "Marco" };
var c2 = new Customer { Name = "Paolo" };
var c3 = new { Name = "Tom", Age = 31 };
var c4 = new { c2.Name, c2.Age };
var c5 = new { c1.Name, c1.Country };
var c6 = new { c1.Country, c1.Name };
1.3 Object and Collection Initializers
//使用Object Initialization Expressions
Customer customer = new Customer { Name = "Marco", Country = "Italy" };
var customers = new []{
new { Name = "Marco", Discount = 4.5 },
new { Name = "Paolo", Discount = 3.0 },
new { Name = "Tom", Discount = 3.5 }
};
1.4 implicative Local Variable Type
var x = 2.3; // double
var s = "sample"; // string
1.5 Extend Method
1.6 Auto property
1.7 Query syntax
2 Linq 高级
2.1 IEnumerable
2.2 IEnumerator
2.3 IQueryable
2.4 IQueryProvider
2.5 Expression Tree
3 Linq To Sql
3.1 What is Linq to Sql
LINQ to SQL is an O/RM (object relational mapping) implementationand which allows you to model a relational database using .NET classes. You can then query the database using LINQ, as well as update/insert/delete data from it.
3.2 Modeling Databases
Use a LINQ to SQL designer that provides an easy way to model and visualize a database as a LINQ to SQL object model.
3.3 Extensibility Partial Class and Method
3.4 Query Update Delete Insert Paging
Paging with Skip()& Take() Operators
varq=query.skip(pagesize*pagenum).take(pagesize)
3.4.1 Query
3.4.2 Upate Delete Insert
3.4.3 Paging
3.5 Use StoreProcedure & User Defined Function
3.6 aspLinqDatasource others UI Controls
3.7 Executing Custom SQL Expressions & DynamicQuery
Custom SQL Expression
DynamicQuery
3.8 Concurrent Conflict
3.9 Using DBContext
3.10 Inheritance & Relationship
3.11 Some Feature:DataLoadOptions & Caching & Lazy Loading &DataContext隔离