Composite Expressions FLinq

When doing linq-to-sql in C #, you can 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 will create 2 separate SQL queries, one with TOP 10 and the other with TOP 3. When playing with Flinq, I see the following:

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 

Don't do the same. Here, he seems to make one complete request, and then performs “accept” client-side calls. The alternative that I see 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 SQL with the appropriate TOP N filter. My problem is that it doesn't seem complicated. I basically have to duplicate the where clause, and may have to duplicate other other subqueries that I can use for the base query. Is there a way for F # to give me something more complicated?

( hubfs, , , # " ", .. , F # .)

+3
1

F #, . ( ) F #. , , :

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

, . F # , .

, , Seq.take, Seq.sortBy . . ( <@ .. @>) operato %, , :

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

op Expr<seq<MyTableRow> -> 'a>. createQuery , . :

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

, #. :

  • LINQ F # F # - , , , . F # _ , , ( _ ). ( Unicode: -)):

    (fun x -> <@ .. %x .. @>)
    
  • LINQ # , , # ( )

+5

Source: https://habr.com/ru/post/1742860/


All Articles