Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 767005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:08:23+00:00 2026-05-14T17:08:23+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T17:08:23+00:00Added an answer on May 14, 2026 at 5:08 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just ignore gen and bin directories. May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer I use this code for the second display in full… May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer GetThreadTimes() provides this info. Very hard to use however since… May 15, 2026 at 2:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.