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

  • SEARCH
  • Home
  • 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 7839943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:36:23+00:00 2026-06-02T15:36:23+00:00

Is it possible to stop a query after the first time the where condition

  • 0

Is it possible to stop a query after the first time the where condition is true?

I want to get only the first record which it’s time is bigger than timeParameter. what I have now is:

var records = from rec in table
              where rec.time > timeParameter
              select rec;
return records.FirstOrDefault();

The time column in the database is ordered by ascending so the first time the where condition is true there is no need to keep querying.
I have lots of rows in the database so I want the query to stop as soon as possible.

  • 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-06-02T15:36:25+00:00Added an answer on June 2, 2026 at 3:36 pm

    This should stop, actually. records is an IQueryable, so will not run until you request data (via FirstOrDefault), which will add the appropriate stop.

    Basically, when you assign the records variable to the LINQ statement, it is merely loaded into memory as what the SQL WILL be. However, it does not evaluate anything until you actually make a call to access the data. At that point, the SQL will be modified appropriately based on whatever extension method you are using (FirstOrDefault in this case) and actually executed.

    However, as Andrew Barber points out, your query needs to actually state the order by or else it will run without it. (Unless the table is truly sorted on the DB side as DangerMonkey counterpoints)

              from rec in table
              where rec.time > timeParameter
              order by rec.time
              select rec;
    

    PS. If you want to really figure out how this works, then you can look into Expression Trees and deferred execution

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

Sidebar

Related Questions

is it possible to stop the submission of a form,after submit button has been
I have a simple query which loops and I want to see the PRINT
Is it possible to stop the automatic preventDefault() from applying in a simple Jquery
Is it possible to stop all started services when the user hits the Home
Given a certain set of parameters is it possible to stop a set of
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Best way to stop SQL Injection in PHP I need to secure
Possible Duplicate: Dispatch queues: How to tell if they're running and how to stop
I mean hypothetically is it possible to control query execution? For example I got

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.