James
James

Reputation: 53

Retrieving top 50 rows from Table using LINQ

Am new to LINQ, and am trying to retrieve the top 50 rows of a particular table.

In SQL Server using an actual query i coudl say "Select TOP 50 from Transactions" , but not sure how i need to do that with LinQ

Any pointers that could help ?

Thanks !

Upvotes: 5

Views: 9841

Answers (3)

helios456
helios456

Reputation: 1644

Something like this would do it.

collection = (from e in table select e).Top(50)

EDIT: Oops, I knew it didn't look right.

collection = (from e in table select e).Take(50)

Upvotes: 0

Kelsey
Kelsey

Reputation: 47726

Here is a basic example doing a select with a where and getting 50 records:

var transactions = (from t in db.Transactions
    where t.Name.StartsWith("A")
    select t).Take(50);

Using other syntax:

var transactions = db.Transactions.Where(t => t.Name.StartsWith("A")).Take(50);

Upvotes: 3

Keltex
Keltex

Reputation: 26426

Like this:

var list = db.Transactions.Take(50);

Of course this doesn't include any ordering or sorting which your query will probably need.

Upvotes: 1

Related Questions