阅读部分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