通过在 EF 中指定范围从 ObjectSet 获取对象
我正在尝试 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 queryselect * 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我终于找到了一种我认为相当不错的方法。这就是我所做的。
我使用 IQueryable 中的 Skip 和 Take 方法根据页面索引跳过并获取对象。
所以我使用了以下代码:
这是一种方法。
如果有人觉得这不是一个好的解决方案,非常欢迎您想出其他我可以替换的东西。
更新代码
根据 Yury Tarabanko 的建议,我将代码更改如下:
感谢那些花时间阅读我的问题的人。
恩克,
尼克...
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:
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:
Thanks for those who took time to read my question.
Thnq,
NiK...