教我如何改进我的F# linq2sql系统
这里我使用的是linq2sql,但我认为我有问题。
这里的主要问题是通过ID进行访问,例如,我正在制作2嵌入式程序,但是我得到了非常可怕的linq2 sql查询,因为我不知道是否有其他方法或方法来实现它.
member X.deltaArchive() = // Reacting on delta limits
seq { for a in db.ArchiveAnalogs do
for d in db.Deltas do
if a.ID = d.ID then
if a.Value > d.DeltaLimit then
yield d.AboveMessage
else if a.Value < d.DeltaLimit then
yield d.BelowMessage
} |> Array.ofSeq因此,完整的问题是:在不使用嵌入式循环来发现id一致性的情况下,有什么方法可以做到这一点吗?
谢谢。
添加:
使用:
<@ seq {for a in db.ArchiveAnalogs do
for d in db.Deltas do
if a.ID = d.ID then
if a.Value > d.DeltaLimit then
yield a.Date, d.AboveMessage
else if a.Value < d.DeltaLimit then
yield a.Date, d.BelowMessage}
@> |> query |> Array.ofSeqgot错误:
The following construct was used in query but is not recognised by the F#-to-LINQ query translator:
Call (None,
System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.DateTime,System.String]] Singleton[Tuple`2](System.Tuple`2[System.DateTime,System.String]),
[NewTuple (PropertyGet (Some (a), System.DateTime Date, []),
PropertyGet (Some (d), System.String AboveMessage, []))])
This is not a valid query expression. Check the specification of permitted queries and consider moving some of the query out of the quotation副标题:我必须找到解决方案,因为这是关于"F# linq2sql“的第一个google链接。
发布于 2011-02-17 11:23:56
首先,您编写的片段并不是真正使用LINQ。您正在内存中运行整个处理过程,因为F#不根据类型选择查询运算符(就像C#那样)。需要显式标记查询才能在SQL上运行它:
#r "FSharp.PowerPack.Linq.dll"
open Microsoft.FSharp.Linq
<@ seq { for a in db.ArchiveAnalogs do ... } @> |> query编写所需内容的另一种方法是使用Query.join函数(来自PowerPack)。我相信以下几点应该能奏效:
<@ join db.ArchiveAnalogs db.Deltas (fun a -> a.ID) (fun d -> d.ID) (fun a d ->
if a.Value > d.DeltaLimit then
yield d.AboveMessage
else if a.Value < d.DeltaLimit then
yield d.BelowMessage ) @> |> query(尽管如此,我认为使用join和嵌套for并没有什么区别--如果您在SQL上运行它,那么它可能会优化它以加入其中)。
https://stackoverflow.com/questions/5025936
复制相似问题