I have a question regarding LINQ's Take() Method.
I have a somewhat large table I'm querying in my web app, and I only want to return say N number of rows from the table.
I've read through the MSDN documentation, but I can't see where it states if
Take() first pulls all of the records from the DB or does it behave similar to SQL Server's TOP.
I'm worried if I call
Take() will it pull all records then get the top N number of records OR will it behave as expected and retrieve only the N number of records directly
Best How To :
See Return Or Skip Elements in a Sequence.
Take(N) will add
TOP N to your SQL and only retrieve
For example (using my own SQL Server 2014 with EF 6.1):
var query = await dbContext.Lookup
.Where(w => w.LookupCd == '1')
Generates this SQL:
SELECT TOP (10)
[Extent1].[LookupId] AS [LookupId],
[Extent1].[LookupTypeId] AS [LookupTypeId],
[Extent1].[LookupCd] AS [LookupCd],
[Extent1].[LookupName] AS [LookupName],
[Extent1].[LookupDescription] AS [LookupDescription]
FROM [dbo].[Lookup] AS [Extent1]
WHERE '1' = [Extent1].[LookupCd]
Use the SQL Profiler (if you're using SQL Server) if you want to be sure what SQL your LINQ is generating. This is always a good practice with any LINQ you write.