我正在关注一个关于查询表达式的书中的示例。我没有完全按照本书中的内容输入,并且令人惊讶的是,即使对于F#中的内连接查询,序列的顺序也很重要。这本书根本没有提到有关订单的任何内容,msdn只说订单很重要。但是为什么即使对于内部联接也很重要?
这不编译,它说namespace or module fa is not defined
;同样适用于f
:
query { for f in QuerySource.films do
join fa in QuerySource.filmActors on (fa.FilmId = f.Id)
join a in QuerySource.actors on (fa.ActorId = a.Id)
select (f.Name, f.ReleaseYear, a.LastName, a.FirstName)
}
但是这样做:
query { for f in QuerySource.films do
join fa in QuerySource.filmActors on (f.Id = fa.FilmId)
join a in QuerySource.actors on (fa.ActorId = a.Id)
select (f.Name, f.ReleaseYear, a.LastName, a.FirstName)
}
EDIT1:不要认为这很重要,但为了澄清这里是QuerySource
模块
module QuerySource =
open System
type Film = {Id : int; Name: string; ReleaseYear : int; Gross : Nullable<float>}
override x.ToString() = sprintf "%s (%i)" x.Name x.ReleaseYear
type Actor = {Id : int; FirstName : string; LastName : string}
override x.ToString() = sprintf "%s %s" x.LastName x.FirstName
type FilmActor = {FilmId : int; ActorId : int}
let films =
[ {Id = 1; Name = "The Terminator"; ReleaseYear = 1984; Gross = Nullable 38400000.0}
{Id = 2; Name = "Predator"; ReleaseYear = 1987; Gross = Nullable 59735548.0}
{Id = 3; Name = "Commando"; ReleaseYear = 1985; Gross = Nullable 38122105.0}
{Id = 4; Name = "The Running Man"; ReleaseYear = 1987; Gross = Nullable<float>()}
{Id = 5; Name = "Conan the Destroyer"; ReleaseYear = 1984; Gross = Nullable<float>()}
]
let actors = [{Id = 1; FirstName = "Arnold"; LastName = "Schwarzenegger"}
{Id = 2; FirstName = "Linda"; LastName = "Hamilton"}
{Id = 3; FirstName = "Carl"; LastName = "Weathers"}
{Id = 4; FirstName = "Jesse"; LastName = "Ventura"}
{Id = 5; FirstName = "Vernon"; LastName = "Wells"}
]
let filmActors =
[ {FilmId = 1; ActorId = 1}
{FilmId = 1; ActorId = 2}
{FilmId = 2; ActorId = 1}
{FilmId = 2; ActorId = 3}
{FilmId = 2; ActorId = 4}
{FilmId = 3; ActorId = 1}
{FilmId = 3; ActorId = 5}
{FilmId = 4; ActorId = 1}
{FilmId = 4; ActorId = 4} // internationally omitted for filmid = 5
]`
答案 0 :(得分:-3)
订单可能在性能方面具有重要意义。在连接表具有显着不同大小的情况下,查询将在开始第二个表之前开始在其中一个表中搜索所有对应值。如果它从较小的表开始,它会更快完成。