linq中文教程 十八

2021-05-22 14:31:14 字數 3818 閱讀 8579

描述:查詢城市是a打頭和城市包含a的顧客並按照顧客名字排序,相同的顧客資訊不會過濾

查詢句法:

var 連線並且不過濾相同項= (from c in ctx.customers where c.city.contains("a") select c).concat

(from c in ctx.customers where c.contactname.startswith("a") select c).orderby(c => c.contactname);

對應sql:

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

from (

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

from (

select [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]

where [t0].[city] like @p0

union all

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

from [dbo].[customers] as [t1]

where [t1].[contactname] like @p1

) as [t2]

) as [t3]

order by [t3].[contactname]

-- @p0: input string (size = 3; prec = 0; scale = 0) [%a%]

-- @p1: input string (size = 2; prec = 0; scale = 0) [a%]

取相交項

描述:查詢城市是a打頭的顧客和城市包含a的顧客的交集,並按照顧客名字排序

查詢句法:

var 取相交項= (from c in ctx.customers where c.city.contains("a") select c).intersect

(from c in ctx.customers where c.contactname.startswith("a") select c).orderby(c => c.contactname);

對應sql:

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

from (

select distinct [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 (exists(

select null as [empty]

from [dbo].[customers] as [t2]

where ([t1].[customerid] = [t2].[customerid]) and ([t2].[contactname] like @p0)

)) and ([t1].[city] like @p1)

order by [t1].[contactname]

-- @p0: input string (size = 2; prec = 0; scale = 0) [a%]

-- @p1: input string (size = 3; prec = 0; scale = 0) [%a%]

排除相交項

描述:查詢城市包含a的顧客並從中刪除城市以a開頭的顧客,並按照顧客名字排序

查詢句法:

var 排除相交項= (from c in ctx.customers where c.city.contains("a") select c).except

(from c in ctx.customers where c.contactname.startswith("a") select c).orderby(c => c.contactname);

對應sql:

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

from (

select distinct [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 (not (exists(

select null as [empty]

from [dbo].[customers] as [t2]

where ([t1].[customerid] = [t2].[customerid]) and ([t2].[contactname] like @p0)

))) and ([t1].[city] like @p1)

order by [t1].[contactname]

-- @p0: input string (size = 2; prec = 0; scale = 0) [a%]

-- @p1: input string (size = 3; prec = 0; scale = 0) [%a%]

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...