The first statement does not execute the actual SQL query, it only builds part of the query you intend to run.
It is when you call
query.Count() that the first will be executed
SELECT COUNT(*) FROM Table WHERE Something = something
query.Skip().Take() won't execute the query either, it is only when you try to enumerate the results(doing a foreach over
paged or calling
.ToList() on it) that it will execute the appropriate SQL statement retrieving only the rows for the page (using ROW_NUMBER).
If watch this in the SQL Profiler you will see that exactly two queries are executed and at no point it will try to retrieve the full table.
Be careful when you are using the debugger, because if you step after the first statement and try to look at the contents of
query that will execute the SQL query. Maybe that is the source of your misunderstanding.
I believe Entity Framework might structure the SQL query with the appropriate conditions based on the linq statements. (e.g. using ROWNUMBER() OVER ...).
I could be wrong on that, however. I'd run SQL profiler and see what the generated query looks like.
// Execute the query var query = db.Entity.Where(e => e.Something == something);
For your information, nothing is called after the first statement.
// Get the total num records var total = query.Count();
This count query will be translated to SQL, and it'll make a call to database. This call will not get all records, because the generated SQL is something like this:
SELECT COUNT(*) FROM Entity where Something LIKE 'something'
For the last query, it doesn't get all the records neither. The query will be translated into SQL, and the paging run in the database.
Maybe you'll find this question useful: efficient way to implement paging
- Efficiently paging large data sets with LINQ
- Memory/Efficiency with Linq and large data sets
- How to deal with large result sets with Linq to Entities?
- Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)
- Using ToList() on Enumerable LINQ query results for large data sets - Efficiency Issue?
- Linq Filtering items with ForEach crashes for large data set
- Linq to sql as object data source - designer problem with partial classes
- Does XML + LINQ will give me good performance with large amounts of data in C#?
- How to swap the data source associated with a Linq query?
- C# LINQ join with conditional where clause on two different data sets
- Kendo Paging with Linq to SQL Classes, Query All Data Very Slow
- Enumerating large data sets multiple times using LINQ to Entities
- Performance issue with LINQ when getting recent data by hours on a large set
- Conditional LINQ query on self-joining table with 2 sets of data
- Linq Data Source WhereParameters with "\" in value
- Linq To Entities, Select multiple sets of data with one query
- Matching two data sets with C# and LinQ
- c# Linq or code to extract groups from a single list of source data
- Querying external data source with LINQ
- Filter data sets with LINQ by year of a date
- Performance issue in IEnumerable type when querying large amount of data with LINQ
- ASP.NET MVC paging large volumes of data using LINQ
- Data paging in linq without removing records from the data source
- Paging with LINQ for objects
- Paging a collection with LINQ
- The data source does not support server-side data paging
- Convert CollectionBase to List or data type usable with Linq
- find non intersecting data set with linq
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- How to search Hierarchical Data with Linq
More Query from same tag
- Generic method with dynamically selected parameter in predicate
- Linq to Entities, Master Detail into DataContracts Query
- Linq, join, group, count > select more values
- Linq with different data types
- How to use a LINQ where clause on a list of Func delegates
- How does VS Code metrics do the math
- linq to sql relationships
- Filtering a deep json hierarchy with Linq
- Create runtime predicate with information from Expression<Func<T, object>>
- offset latitude slightly if not unique
- Finding pattern occurence count in List with Linq
- VB.NET Linq query Left outer join left is null
- Selecting Distinct using LINQ with CRM
- Assign COUNT() to model field when projecting into a Model
- Linq to SQL to return multiple counts from query not returning correct results
- Multiple WHERE. Is there a possibility that a specific WHERE can only be executed in LINQ?
- Group data having unique keys and values
- Version of iteration which takes "groups" of elements
- How can I write this query in linq to sql?
- Group By from DataTable using linq
- How to replace a character in string using LINQ
- XML File Update
- How do I find the count of overlapping properties in SQL/LINQ query between two tables?
- Linq skip if not first page
- How to find duplicate property values in an IList of objects and merge them?
- Recursive linq to get infinite children
- SQL to LINQ handling Match in Left outer JOIN
- How to assign value from properties to enum in Linq
- LINQ: Evaluating a function where array elements are matched by index
- Why is this linq expression not working?