views:

118

answers:

1

Hi all, I am trying out EF 4.0.I have an Employee Object and using EF I am getting the Employee ObjectSet by simply calling

Context.Employees

Now the above call will spit following sql query select * from Employees

The above query works fine and I don't have any complains on it however as you know this will not be performant enough if you have few millions of records in the table and it will definitely effect the performance.

So then I am trying to figure out a way to give a range to my ObjectSet where I can say something like get me records 30 to 60 from the Employee ObjectSet.

Is there a way to do something like this.

Any suggestions will be deeply appreciated.

Update: I am trying to do this to get 20 Employees (page size) back based on the page index.

Thanks in advance. NiK...

+4  A: 

Ok, 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 take 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 anyone feel that this is not a good solution you are more than welcome to come up with something else which I can replace with.

Updated Code As per Yury Tarabanko's suggestion I changed my code as follows:

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

Thanks for those who took time to read my question.

Thnq, NiK...

NiK
By the way, you can use `OrderBy(e => e.CreatedDate)` to avoid 'magic strings'.
Yury Tarabanko
Thanks a lot Yury.
NiK
huhu works.Thanks for the answer.