简体   繁体   English

使用查询表达式与Seq模块?

[英]Using query expression vs Seq modules?

Are there any advantages in using the query expression over Sequence modules when manipulating data retrieved via FSharp.Data.SqlClient? 在操作通过FSharp.Data.SqlClient检索的数据时,在Sequence模块上使用查询表达式有什么优势吗?

For example: 例如:

query{
    for row in SelectAllCategories.Execute() do
    where row.Id = 1
    select {
        Id = row.Id;
        Category = row.CategoryName
    }
}

versus

SelectAllCategories.Execute()
|> Seq.filter (fun x -> x.Id = 1)
|> Seq.map (fun x ->
                {
                   Id = x.Id;
                   Category = x.CategoryName
                }

For that matter you can even consider LINQ as well. 就此而言,您甚至可以考虑使用LINQ。 So what are the advantages if any, specifically in regards to FSharp.Data.SqlClient? 那么有什么好处,特别是关于FSharp.Data.SqlClient?

In this particular case FSharp.Data.SqlClient provides the default IEnumerable<ProvidedType>.Record result type, not IQueryable<ProvidedType> of any sort. 在这种特殊情况下, FSharp.Data.SqlClient提供默认的IEnumerable<ProvidedType>.Record结果类型,而不是任何类型的IQueryable<ProvidedType> All communications with SQL engine are opaque to query expression, being encapsulated into provided SqlCommandProvider.Execute() method. 与SQL引擎的所有通信对查询表达式都是不透明的,被封装到提供的SqlCommandProvider.Execute()方法中。 So any potential benefits of query {...} using Linq-to-Sql are not in play here. 所以使用Linq-to-Sql的query {...}任何潜在好处都不在这里。

Hence, I'd not be surprised that the case with functions from Seq module would yield better performance having less overhead, than underlying desugared machinery associated with query expressions. 因此,与Seq模块中的函数相比,与查询表达式相关的底层desugared机制产生的开销更少,我不会感到惊讶。 No advantages of using the query expression here. 这里没有使用查询表达式的优点。

Query builder provides a way to construct IQueryables in F#, Seq module functions work on IEnumerables (that seq type is an alias for). Query构建器提供了一种在F#中构造IQueryables的方法, Seq模块函数在IEnumerables工作( seq类型是别名)。 Those are general .NET interfaces rather than an F# thing, and the differences between them are well outlined for example here . 这些是通用的.NET接口而不是F#的东西,它们之间的差异在这里有很好的概述。

Also, apart from Seq module functions you also have the seq builder: 此外,除了Seq模块功能,您还有seq构建器:

seq {
   for row in SelectAllCategories.Execute() do 
       if row.Id = 1 then 
           yield { Id = row.Id; Category = row.CategoryName }
}

It's roughly equivalent to using Seq module functions, so the choice of one over the other is mostly a style or convenience decision. 它大致相当于使用Seq模块功能,因此选择一个而不是另一个主要是风格或便利决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM