聯接是指將乙個資料來源物件與另乙個資料來源物件進行關聯或者聯合的操作。這兩個資料來源物件通過乙個共同的值或者屬性進行關聯。
linq有兩個聯接操作符:join和groupjoin。
join操作符類似於t-sql中的inner join,它將兩個資料來源相聯接,根據兩個資料來源中相等的值進行匹配。例如,可以將產品表與產品類別表相聯接,得到產品名稱和與其相對應的類別名稱。以下的**演示了這一點:
//查詢語法
var query =
(from p in db.products
join c in db.categories on p.categoryid equals c.categoryid
where p.categoryid == 1
select new ).tolist();
//方法語法
var q =
db.products
.join
(db.categories,
p => p.categoryid,
c => c.categoryid,
(p, c) => new
).where(p => p.categoryid == 1)
.tolist();
groupjoin操作符常應用於返回「主鍵物件-外來鍵物件集合」形式的查詢,例如「產品類別-此類別下的所有產品」。以下的**演示了這一點:
//查詢語法
var query =
(from c in db.categories
join p in db.products on c.categoryid equals p.categoryid into r
select new
).tolist();
//方法語法
var q =
db.categories
.groupjoin
(db.products,
c => c.categoryid,
p => p.categoryid,
(c, p) => new
).tolist();
生成的sql:
select
[project1].[categoryid] as [categoryid],
[project1].[categoryname] as [categoryname],
[project1].[c1] as [c1],
[project1].[productid] as [productid],
[project1].[productname] as [productname],
[project1].[supplierid] as [supplierid],
[project1].[categoryid1] as [categoryid1],
[project1].[quantityperunit] as [quantityperunit],
[project1].[unitprice] as [unitprice],
[project1].[unitsinstock] as [unitsinstock],
[project1].[unitsonorder] as [unitsonorder],
[project1].[reorderlevel] as [reorderlevel],
[project1].[discontinued] as [discontinued]
from ( select
[extent1].[categoryid] as [categoryid],
[extent1].[categoryname] as [categoryname],
[extent2].[productid] as [productid],
[extent2].[productname] as [productname],
[extent2].[supplierid] as [supplierid],
[extent2].[categoryid] as [categoryid1],
[extent2].[quantityperunit] as [quantityperunit],
[extent2].[unitprice] as [unitprice],
[extent2].[unitsinstock] as [unitsinstock],
[extent2].[unitsonorder] as [unitsonorder],
[extent2].[reorderlevel] as [reorderlevel],
[extent2].[discontinued] as [discontinued],
case when ([extent2].[productid] is null) then cast(null as int) else 1 end as [c1]
from [dbo].[categories] as [extent1]
left outer join [dbo].[products] as [extent2] on [extent1].[categoryid] = [extent2].[categoryid]
) as [project1]
order by [project1].[categoryid] asc, [project1].[c1] asc
分組是根據乙個特定的值將序列中的元素進行分組。linq只包含乙個分組操作符:groupby。
下面的示例中使用了產品表,以categoryid作為分組關鍵值,按照產品類別對產品進行了分組。
//查詢語法
var query =
(from p in db.products
group p by p.categoryid).tolist();
//方法語法
var q =
db.products
.groupby(p => p.categoryid)
.tolist();
執行groupby得到的序列中包含的元素型別為igrouping,其key屬性代表了分組時使用的關鍵值,遍歷igrouping元素可以讀取到每乙個t型別。在此示例中,對應的元素型別為igrouping,其key屬性即為類別id,遍歷它可以讀取到每乙個產品物件。
串聯是乙個將兩個集合聯接在一起的過程。在linq中,這個過程通過concat操作符來實現。
在下面的示例中,將會把類別名稱串聯在產品名稱之後:
//方法語法
var q =
db.products
.select(p => p.productname)
.concat
(db.categories.select(c => c.categoryname)
).tolist();
生成的sql:
select
[unionall1].[productname] as [c1]
from (select
[extent1].[productname] as [productname]
from [dbo].[products] as [extent1]
union all
select
[extent2].[categoryname] as [categoryname]
from [dbo].[categories] as [extent2]) as [unionall1]
1 投影操作符 LINQ標準查詢操作符
public class select linq var methodsyntex contact.select c new where con con.firstname.startswith s console.writeline query syntex foreach var item in...
Linq的查詢操作符
linq有表示式語法和呼叫方法的語法。兩者是可以結合使用,通常情況下也都是結合使用。表示式語法看上去比較清晰而呼叫方法的語法實現的功能更多,在此文章中介紹的是表示式語法。方法語法可以看system.linq等命名空間下的擴充套件方法。linq只能用於實現了ienumerable或ienumerabl...
linq操作符 限定操作符
限定操作符運算返回乙個boolean值,該值指示序列中是否有一些元素滿足條件或者是否所有元素都滿足條件。一 all操作符 all方法用來確定是否序列中的所有元素都滿足條件。看下面的例子 1 using system 2using system.collections.generic 3using s...