linq中文教程 十五

2021-05-22 14:31:14 字數 2361 閱讀 7532

描述:查詢顧客的國家、城市和訂單數資訊,要求國家是法國並且訂單數大於5

查詢句法:

var 多條件= from c in ctx.customers

where c.country == "france" && c.orders.count > 5

select new ;

對應sql:

select [t0].[country], [t0].[city], (

select count(*)

from [dbo].[orders] as [t2]

where [t2].[customerid] = [t0].[customerid]

) as [value]

from [dbo].[customers] as [t0]

where ([t0].[country] = @p0) and (((

select count(*)

from [dbo].[orders] as [t1]

where [t1].[customerid] = [t0].[customerid]

)) > @p1)

-- @p0: input string (size = 6; prec = 0; scale = 0) [france]

-- @p1: input int32 (size = 0; prec = 0; scale = 0) [5]

orderby

描述:查詢所有沒有下屬雇員的雇用年和名,按照雇用年倒序,按照名正序

查詢句法:

var 排序= from emp in ctx.employees

where emp.employees.count == 0

orderby emp.hiredate.value.year descending, emp.firstname ascending

select new ;

對應sql:

select datepart(year, [t0].[hiredate]) as [value], [t0].[firstname]

from [dbo].[employees] as [t0]

where ((

select count(*)

from [dbo].[employees] as [t1]

where [t1].[reportsto] = [t0].[employeeid]

)) = @p0

order by datepart(year, [t0].[hiredate]) desc, [t0].[firstname]

-- @p0: input int32 (size = 0; prec = 0; scale = 0) [0]

分頁

描述:按照每頁10條記錄,查詢第二頁的顧客

查詢句法:

var 分頁= (from c in ctx.customers select c).skip(10).take(10);

對應sql:

select top 10 [t1].[customerid], [t1].[companyname], [t1].[contactname], [t1].[contacttitle], [t1].[address], [t1].[city], [t1].[region], [t1].[postalcode], [t1].[country], [t1].[phone], [t1].[fax]

from (

select row_number() over (order by [t0].[customerid], [t0].[companyname], [t0].[contactname], [t0].[contacttitle], [t0].[address], [t0].[city], [t0].[region], [t0].[postalcode], [t0].[country], [t0].[phone], [t0].[fax]) as [row_number], [t0].[customerid], [t0].[companyname], [t0].[contactname], [t0].[contacttitle], [t0].[address], [t0].[city], [t0].[region], [t0].[postalcode], [t0].[country], [t0].[phone], [t0].[fax]

from [dbo].[customers] as [t0]

) as [t1]

where [t1].[row_number] > @p0

-- @p0: input int32 (size = 0; prec = 0; scale = 0) [10]

linq中文教程 二

what s linq?language integrated query 是也。說得再明白一些,這是程式語言的一種新特性,能夠將資料查詢語句整合到程式語言中。目前,linq 支援的語言有 c 和vb。為啥會有 linq 主要還是因為現在的資料格式越來越多,資料庫 xml 陣列 雜湊表 每一種都有自...

linq中文教程 五

自動屬性 public class person public int age public person person p new person p.username aa console.writeline p.username 意義不是很大,純粹解決機械勞動。編譯器自動為你生成get set操...

linq中文教程 七

datacontext datacontext型別 資料上下文 是system.data.linq命名空間下的重要型別,用於把查詢句法翻譯成sql語句,以及把資料從資料庫返回給呼叫方和把實體的修改寫入資料庫。datacontext提供了以下一些使用的功能 以日誌形式記錄datacontext生成的s...