Upgrade project to 2008
enabled LINQ
1. Convert project soln to VS 2008, open it by VS 2008.
2. Right click project > properties.
3. Compile > Option Infer > ON .
4. Advanced compiler options > Target framework > .NET framework
3.5 > this will add System.Core 3.5
5. Reference > Imported namespace > System.Data.Linq (allow write
Linq against object) & System.
6. Add reference System.xml.Linq > allow .dbml (Linq to SQL Class)
LINQ TO SQL (Allow
developer write query against the collection of object)
1. Create Linq to SQL Class , project> add > new item > Linq
to SQL.
2. Name it ( ChianWind) and it will create chianwind.dbml.
3. In its designer, drag tables into panel and the relationship is created
automatically.
Also, Conn String in web.config will be created too.
4. To create store procedure, drag sp into designer right panel. DONE
Example
Dim db as NEW ChianWindDataContext
( product 1-N ProdcutVendor )
Dim pros = From p In db.ProductVendors _
Where p.Product.SafetyStockLevel = 800 and p.StartsWith("A")
Select p.Product.ProductID,
Maximum = p.MaxOrderQty,
p.Product.Name,
p.Product.ProductNumber
OR
Select New ProductInfo with ( .Name=p.Product.Name , .Number= p.Product.ProductNumber
)
* .Name and .Number are properties of ProductInfo class
GV.DataSource = pros.Skip(startrow).Take(10) //skip and take are extension
method
GV.DataBind() //
OR
Dim files = From fs In My.Computer.FileSystem.GetFiles(CurDir) _
Select fs
Dim fileinfo = From fi In files _
Select My.Computer.FileSystem.GetFileInfo(fi)
OR
Dim int As Integer() = {0, 1, 2, 3, 4, 6, 5, 9, 8}
Dim nums As List(Of Integer) = New List(Of Integer)
nums.AddRange(int)
Dim ls As List(Of Integer) = (From p In int _
Where p > 0 _
Select p).ToList
Response.Write("Average is " & ls.Average() & "
count is " & nums.Count)
LINQ Using Stored Procedure
1. Drag the stored procedure or functions from Server explorer to dbml
Panel.
2. dim db as new databaseContext.
3. db.SPName or db.MyFuct.
Extension Method
- Imports , System.Runtime,CompilerService
- <Extension()>
- has down arrow
- Applied to module or sub , NOT Class
- At least ext method takes 1 parameter
- parameter type determine the accessor.
- Must Public, can be return something OR Nothing
Module ExtMtd
<Extension()> _
' Param takes Object type, any type of data can access it
Public sub Print (Byval o as object)
Response.Write(o.tostring())
End sub
<Extension()> _
' Only integer varaible can access it.
Public Function Increment(ByVal i As integer ) As Integer
Return i +1
End Function
End Module
|