Reputation: 2204
i have following Linq query . its works well but the thing that seems confusing is how does skip()
and take()
function working in linq.
here is my query
(from GRD in _tblAcademicYears.GetQueryable()
where GRD.SchoolID == intSchoolID
select new AcademicYearsModel
{
AcademicYearID = GRD.AcademicYearID,
SchoolID = GRD.SchoolID,
AcademicYearName = GRD.AcademicYearName,
AcademicYearStart = GRD.AcademicYearStart,
AcademicYearEnd = GRD.AcademicYearEnd,
AcademicYearRemarks = GRD.AcademicYearRemarks,
IsActive = GRD.IsActive,
CreatedOn = GRD.CreatedOn,
CreatedBy = GRD.CreatedBy,
ModifiedOn = GRD.ModifiedOn,
ModifiedBy = GRD.ModifiedBy
}
).Where(z => z.AcademicYearName.Contains(param.sSearch) || z.AcademicYearStart.ToString().Contains(param.sSearch)
|| z.AcademicYearEnd.ToString().Contains(param.sSearch) || z.AcademicYearRemarks.Contains(param.sSearch))
.Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();
How this query will get record from data base .
will it get all record from database and then will apply skip()
and take()
.
or it will just get record that are with in limits of skip()
and take()
Upvotes: 0
Views: 2732
Reputation: 30464
The source code of all Linq IEnumerable extensions can be found here: System.Linq.Enumerable
Here you can see how skip and take work
Upvotes: 1
Reputation: 24903
If you asking about LINQ to SQL, you can run a sql-profiler to get query, generated by linq provider.
But I can tell you, LINQ will get only records in limits skip
and take
, using row_number operator in SQL:
The query will be like this (skip 3 and take 3):
SELECT TOP (3)
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
FROM (
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
row_number() OVER (ORDER BY [Extent1].[Name] ASC) AS [row_number]
FROM [dbo].[tec_Stores] AS [Extent1]
) AS [Extent1]
WHERE [Extent1].[row_number] > 3
ORDER BY [Extent1].[Name] ASC
In LINQ to Entities it works different, depending on collection you use.
Upvotes: 2
Reputation: 1088
When you call .Take only, it will just translate to SQL: TOP N syntax
When you call .Skip and .Take together, it will generate at least 2 queries, by using ROWNUMBER to filter out.
So the short answer for your question is: No, it will not get all records from database. it will run a SQL to filter and select.
If you are curious, you can always use SQL profiler or just check the generated SQL in the debug mode.
Here is a simple MSDN article explains it https://msdn.microsoft.com/library/bb386988(v=vs.100).aspx
Upvotes: 6