Linq to SQL - Return top n rows

.NetLinq to-Sql

.Net Problem Overview


I want to return the TOP 100 records using Linq.

.Net Solutions


Solution 1 - .Net

Use the Take extension method.

var query = db.Models.Take(100);

Solution 2 - .Net

You want to use Take(N);

var data = (from p in people
           select p).Take(100);

If you want to skip some records as well you can use Skip, it will skip the first N number:

var data = (from p in people
           select p).Skip(100);

Solution 3 - .Net

Example with order by:

var data = (from p in db.people  
            orderby p.IdentityKey descending 
            select p).Take(100); 

Solution 4 - .Net

Use Take() extension

Example:

var query = (from foo in bar).Take(100)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjinsungyView Question on Stackoverflow
Solution 1 - .NettvanfossonView Answer on Stackoverflow
Solution 2 - .NetLukaszView Answer on Stackoverflow
Solution 3 - .NetMichael FreidgeimView Answer on Stackoverflow
Solution 4 - .NetScrappydogView Answer on Stackoverflow