Bcpouli
Bcpouli

Reputation: 225

How to limit the number of elements in a checkboxlist?

I have a checkboxlist in my C#/asp.net project and I'm populating it with a dataTable that gets data from a query to my database. The query returns a large amount of data and I want to restrict the number of elements that it shows initially before I filter the data. (To, say, the top 1000). How would I go about doing this?

Upvotes: 1

Views: 357

Answers (2)

System Down
System Down

Reputation: 6270

There are two places where you can limit the number of data.

In the database (assuming you use SQL Server) you can modify the query to return the top 1000 rows.

SELECT TOP 1000 * FROM SomeTable

Or you can filter the data after it arrives using Linq.

var newData = dataTable.AsEnumerable().Take(1000);

I would prefer the first method, so you don't truck around useless data. But the second definitely works as well if you need that data elsewhere.

Upvotes: 2

PinnyM
PinnyM

Reputation: 35531

You can use the Take<> generic IEnumerable method:

var data = someQuery.Exec();
var limitedData = data.Take(1000).ToArray();

Upvotes: 0

Related Questions