閱讀部分7.16.2。所有規則都在那裏。
讓我們來看看它。您有:
from r in this.bdd.Rights
join f in this.bdd.Folders on r.RightFolderId equals f.FolderId
join rs in this.bdd.RightSpecs on r.RightSpecId equals rs.SpecIdRight
where r.RightUserId == userId
where rs.SpecRead == true
where rs.SpecWrite == true
select f;
細則中指出
與連接子句的查詢表達式沒有到後面跟着一個非SELECT子句
from x1 in e1
join x2 in e2 on k1 equals k2
...
以外的東西翻譯分成
from * in (e1)
. Join(e2 , x1 => k1 , x2 => k2 , (x1 , x2) => new { x1 , x2 })
...
好了,我們開始通過翻譯查詢到
from * in
(this.bdd.Rights)
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(*, f)=> new {r, f})
join rs in this.bdd.RightSpecs on r.RightSpecId equals rs.SpecIdRight
where r.RightUserId == userId
where rs.SpecRead == true
where rs.SpecWrite == true
select f;
現在同樣的規則再次適用;我們有一個查詢表達式,其中有一個沒有into的連接子句,後面跟着一個select以外的東西。如此變成:
from ** in
((this.bdd.Rights)
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(r, f)=> new {r, f}))
.Join(
this.bdd.RightSpecs,
*=>r.RightSpecId,
rs=>rs.SpecIdRight,
(*, rs)=> new {*, rs})
where r.RightUserId == userId
where rs.SpecRead == true
where rs.SpecWrite == true
select f;
接下來的規則是什麼?諮詢規格:用
查詢表達式where子句
from x in e
where f
...
被翻譯成
from x in (e) . Where (x => f)
...
OK,讓我們應用這一轉變三次,獲得
from ** in
(((((this.bdd.Rights)
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(r, f)=> new {r, f}))
.Join(
this.bdd.RightSpecs,
*=>r.RightSpecId,
rs=>rs.SpecIdRight,
(*, rs)=> new {*, rs}))
.Where(**=>r.RightUserId == userId))
.Where(**=>rs.SpecRead == true))
.Where(**=>rs.SpecWrite == true)
select f;
現在呢?諮詢規格:
from x in e select v
被翻譯成
(e) . Select (x => v)
所以上面的代碼被轉換爲
((((((this.bdd.Rights)
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(r, f)=> new {r, f}))
.Join(
this.bdd.RightSpecs,
*=>r.RightSpecId,
rs=>rs.SpecIdRight,
(*, rs)=> new {*, rs}))
.Where(**=>r.RightUserId == userId))
.Where(**=>rs.SpecRead == true))
.Where(**=>rs.SpecWrite == true))
.Select(**=>f);
其中*
和**
是透明的標識符。因此,這是再進一步轉化爲
((((((this.bdd.Rights)
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(r, f)=> new {r, f}))
.Join(
this.bdd.RightSpecs,
t1=>t1.r.RightSpecId,
rs=>rs.SpecIdRight,
(t1, rs)=> new {t1, rs}))
.Where(t2=>t2.t1.r.RightUserId == userId))
.Where(t2=>t2.rs.SpecRead == true))
.Where(t2=>t2.rs.SpecWrite == true))
.Select(t2=>t2.t1.f);
我們介紹了一大堆不必要的括號在那裏。我們可以把它們拿出來說,這相當於
this.bdd.Rights
.Join(
this.bdd.Folders,
r=>r.RightFolderId,
f=>f.FolderId,
(r, f)=> new {r, f})
.Join(
this.bdd.RightSpecs,
t1=>t1.r.RightSpecId,
rs=>rs.SpecIdRight,
(t1, rs)=> new {t1, rs})
.Where(t2=>t2.t1.r.RightUserId == userId)
.Where(t2=>t2.rs.SpecRead == true)
.Where(t2=>t2.rs.SpecWrite == true)
.Select(t2=>t2.t1.f);
很容易。這只是一個簡單的句法重寫,除了對透明標識符進行一些分析之外。
請提供有關「其他語法」的含義的更多信息。 – 2010-05-24 15:51:52
對不起,我的英語太糟糕了! 第一個代碼看起來像一個「SQL查詢」。但是我想要與LINQPad的鏈接使用Lambda語法 – scrat789 2010-05-24 16:28:21