Getting 50 rows from a table using LINQ

I am new to LINQ and I am trying to extract the top 50 rows of a specific table.

In SQL Server, using the actual query, I can say: “Select TOP 50 from transactions”, but not sure how I need to do this with LinQ

Any pointers that could help?

Thanks!

+4
source share
3 answers

Like this:

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

Of course, this does not include the ordering or sorting that your query is likely to require.

0
source

Here is a basic example that selects with where and retrieves 50 entries:

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

Using a different syntax:

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

Something like this would do it.

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

EDIT: Oh, I knew this didn't look right.

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

Source: https://habr.com/ru/post/1308837/


All Articles