Ben Ben - 2 months ago 4
C# Question

How can I get a specific number of records when performing an IEnumarable query?

I have an

IEnumerable
and am currently using it to obtain all of the records in a database.

IEnumerable<Item> items = dbContext.Items.Include(i => i.itemField).AsEnumerable();


Is there any way for me to obtain only a specific amount of items from the database, like with the following pseudo-code?

//This would get records 30 - 40 in the database
items = items.Where(i => i.otherField != null).GetNumberOfRecords(30, 40);

Answer

sure!

you can use:

items  = items.Where(i => i.OtherField != null).Skip(30).Take(10).ToList();

Or no ToList() as you prefer :)

Comments