jinsungy
jinsungy

Reputation: 10835

Linq to SQL - Return top n rows

I want to return the TOP 100 records using Linq.

Upvotes: 88

Views: 100691

Answers (4)

Scrappydog
Scrappydog

Reputation: 2874

Use Take() extension

Example:

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

Upvotes: 2

Michael Freidgeim
Michael Freidgeim

Reputation: 28435

Example with order by:

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

Upvotes: 14

Lukasz
Lukasz

Reputation: 8890

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);

Upvotes: 58

tvanfosson
tvanfosson

Reputation: 532455

Use the Take extension method.

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

Upvotes: 147

Related Questions