tags:

views:

114

answers:

1

When doing linq-to-sql in c#, you could do something like this:

var data = context.MyTable.Where(x => x.Parameter > 10); 

var q1 = data.Take(10); 
var q2 = data.Take(3); 

q1.ToArray(); 
q2.ToArray(); 

This would generate 2 separate SQL queries, one with TOP 10, and the other with TOP 3. In playing around with Flinq, I see that:

let data = query <@ seq { for i in context.MyTable do if x.Parameter > 10 then yield i } @> 

data |> Seq.take 10 |> Seq.toList 
data |> Seq.take 3 |> Seq.toList 

is not doing the same thing. Here it seems to do one full query, and then do the "take" calls on the client side. An alternative that I see used is:

let q1 = query <@ for i in context.MyTable do if x.Param > 10 then yield i } |> Seq.take 10 @> 
let q2 = query <@ for i in context.MyTable do if x.Param > 10 then yield i } |> Seq.take 3 @> 

These 2 generate the SQL with the appropriate TOP N filter. My problem with this is that it doesn't seem composable. I'm basically having to duplicate the "where" clause, and potentially would have to duplicate other other subqueries that I might want to run on a base query. Is there a way to have F# give me something more composable?

(I originally posted this question to hubfs, where I have gotten a few answers, dealing with the fact that C# performs the query transformation "at the end", i.e. when the data is needed, where F# is doing that transformation eagerly.)

+5  A: 

To do this in F#, you'll need to use slightly different approach. Instead of composing the query using method calls (and defered execution), you'll need to construct the quoted F# code. If you simply need to parameterize the code by some numeric argument, you can write a function that runs the query:

let takeData count = 
  <@ seq { for i in context.MyTable do 
             if x.Parameter > 10 then 
               yield i }
     |> Seq.take count @> |> query

This works only in simple cases, because the parameter can be only a number. However, you can also compose F# quotations in a way that allows you to add other operations to a core query.

For example, let's say that you want to append either Seq.take or Seq.sortBy to the core part of the query. This can be done using so called splicing operator. Inside quotations (code within <@ .. @>) you can use a special operato % that allows you to splice another quotation into the one you're building:

let createQuery op = 
  <@ seq { for i in context.MyTable do 
             if x.Parameter > 10 then 
               yield i }
     |> %op @> |> query

Here, the op parameter is of type Expr<seq<MyTableRow> -> 'a>. You can call the createQuery function with some quotation as an argument and it will append the argument after the core query. For example:

createQuery <@ Seq.take 10 @>
createQuery <@ Seq.sortBy (fun x -> x.Parameter) @>

This is actually much more powerful than what C# allows you to do. I wrote two articles about this some time ago:

  • Composing LINQ queries at runtime in F# shows some F# examples - unfortunately, it uses outdated syntax, so it won't directly work, but it should demonstrate the ideas. In older versions F#, you could use _ in the quotation which automatically created a function that took quotation (to be spliced in place of _ as an argument). This would have to be rewritten using (you also don't need funny Unicode characters anymore :-)):

    (fun x -> <@ .. %x .. @>)
    
  • Composing LINQ queries at runtime in C# shows how to provide some additional features that are not directly available in C# (using some tricks)

Tomas Petricek