Get objects from an ObjectSet by specifying a range in EF

I am testing EF 4.0. I have an Employee object and using EF I get an ObjectEll Employee just by calling

Context.Employees

Now the above call will spit after sql request select * from Employees

The above query works fine, and I have no complaints about it, since you know that it is not efficient enough if you have several million records in the table, and this will definitely affect performance.

So, I'm trying to find a way to set a range for my ObjectSet where I can say something like getting 30 to 60 records from an Objectee ObjectSet.

Is there a way to do something like this.

Any suggestions would be greatly appreciated.

Update: I am trying to do this in order to return 20 employees (page size) based on the page index.

Thanks in advance. Nik ...

+5
source share
1 answer

Well, I finally figured out a way to do this, which, I think, is pretty decent. And here is what I did.

I used the Skip and Take methods in IQueryable to skip and migrate objects based on the page index.

So, I used the following code:

var empList = context.Employees.OrderBy("it.CreatedDate").Skip(pageIndex * 20 - 20).Take(20);

This is one way.

If someone thinks this is not a good solution, you can more than come up with something else that I can replace.

Updated code At the suggestion of Yuri Tarabanko, I changed my code as follows:

var empList = context.Employees.OrderBy(x=>x.CreatedDate).Skip(pageIndex * 20 - 20).Take(20);

Thanks to those who took the time to read my question.

Thnq, nik ...

+4
source

All Articles