1

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}
         @> |> query |> 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"

1 Answer 1

2

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).

4
  • what about turples if I need yield a.Date, d.AboveMessage ?
    – cnd
    Feb 18, 2011 at 11:13
  • You should be able to return tuples - at least in LINQ to SQL. (BTW: Sorry, I copied the sample incorrectly when I first posted it) Feb 18, 2011 at 12:49
  • Ye , thank you, But I have a trouble with tuples. check first post "Added". Could that be a PP bug ?
    – cnd
    Feb 18, 2011 at 17:40
  • 1
    @nCdy - Yes, that looks like a bug, but a weird one. Could you try flattening the if so that you don't have nested ifs? Maybe that's causing the problem (report it to fsbugs at microsoft com) Feb 18, 2011 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.