我们不需要猜测,我们可以找出答案。
let sample = Seq.init 10 (fun i -> i * 10) |> Seq.map (fun i -> { age = i })
let sq = query {
for p in sample do
where (p.age > 20)
sortBy p.age
select p
}
sq |> Seq.toList |> ignore
生成的 IL(已清理)看起来像
IL_004e: newobj instance void Program/sq@16::.ctor(class [FSharp.Core]Microsoft.FSharp.Linq.QueryBuilder)
IL_0053: callvirt instance [...] For<class Program/Person,
IL_005d: callvirt instance [...] Where<class Program/Person,
IL_0067: callvirt instance [...] SortBy<class Program/Person,
IL_0071: callvirt instance [...] Select<class Program/Person,
假设我们改变 sortBy 的顺序
let sq = query {
for p in sample do
sortBy p.age
where (p.age > 20)
select p
}
新的 IL 将是:
IL_006c: callvirt instance [...] For<class Program/Person,
IL_0076: callvirt instance [...] SortBy<class Program/Person,
IL_0080: callvirt instance [...] Where<class Program/Person,
IL_008a: callvirt instance [...] Select<class Program/Person,
您可以清楚地看到它遵循您定义查询的确切顺序。这对于 T-SQL 理解无关紧要,因为查询将由表达式访问者翻译,但对于对象查询,查询表达式几乎只是语法糖为你。