通过在 EF 中指定范围从 ObjectSet 获取对象

发布于 2024-09-11 08:24:06 字数 443 浏览 2 评论 0原文

我正在尝试 EF 4.0。我有一个 Employee 对象并使用 EF 我通过简单地调用

Context.Employees

来获取 Employee ObjectSet现在上面的调用将在 sql 查询之后吐出 select * fromEmployees

上面的查询工作正常,我对此没有任何抱怨,但是正如您所知,如果表中有几百万条记录,那么这将不够高效,而且它肯定会影响性能。

因此,我试图找到一种方法来为我的 ObjectSet 指定一个范围,在其中我可以说从 Employee ObjectSet 中获取第 30 到 60 条记录。

有没有办法做这样的事情。

任何建议将不胜感激。

更新: 我试图这样做是为了根据页面索引返回 20 个员工(页面大小)。

提前致谢。 尼克...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-18 08:24:06

好吧,我终于找到了一种我认为相当不错的方法。这就是我所做的。

我使用 IQueryable 中的 Skip 和 Take 方法根据页面索引跳过并获取对象。

所以我使用了以下代码:

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

这是一种方法。

如果有人觉得这不是一个好的解决方案,非常欢迎您想出其他我可以替换的东西。

更新代码
根据 Yury Tarabanko 的建议,我将代码更改如下:

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

感谢那些花时间阅读我的问题的人。

恩克,
尼克...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文