开发者

Working with connected tables using F# linq2sql seqences

Teach me how can I improve my F# linq2sql seqences

here I'm using linq2sql but I think that I got problems with it.

and main problem is access by ID here in example I'm making 2 embedded for but I got very scary linq2 sql queries because I don't know is there some additional methods or ways to make it ...

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

So the complete question is : is there any way to make the same without using embedded cycles to find id conformity ?

Thank you.

Added :

using :

    <@ 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}
         @> |> quer开发者_JS百科y |> Array.ofSeq

got error :

    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

offtopic : I must find solution because this is first google link about "F# linq2sql"


First of all, the snippet you wrote isn't really using LINQ to SQL. You're running the whole processing in memory, because F# doesn't select query operators based on type (as C# does). You need to mark the query explicitly to run it on SQL:

#r "FSharp.PowerPack.Linq.dll"
open Microsoft.FSharp.Linq

<@ seq { for a in db.ArchiveAnalogs do ... } @> |> query

An alternative way to write what you want is to use Query.join function (from PowerPack). I believe the following should do the trick:

<@ 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

(Although, I think that there is really no difference between using join and nested for - If you run this on SQL than it will likely optimize it to join anyway).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜