Is it possible to stop a query after the first condition, when the condition is true? (linq to sql)

Is it possible to stop the request after the first condition is satisfied where?

I want to get only the first record, the time of which is longer than timeParameter. what i have now:

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

The time column in the database is sorted in ascending order, so with the first rule of the where clause there is no need to continue the queries. I have many rows in the database, so I want the request to be stopped as soon as possible.

+3
source share
2 answers

. records IQueryable, , ( FirstOrDefault), .

, records LINQ, , SQL WILL. , . SQL (FirstOrDefault ) .

, , order by, . ( DangerMonkey)

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

PS. , ,

+2

, .

.FirstOrDefault(). SQL-, SELECT TOP 1 ..., , , SQL Server , , .

:

...

, ? , , , . , order by , , . , .

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

All Articles