Linq 选择记录范围

发布于 2024-10-20 20:27:56 字数 275 浏览 1 评论 0原文

    var q = (from Comments in db.tblBlogComments where Comments.blogID == this.ID orderby Comments.date descending select new {
        Comments.userID, Comments.comment, Comments.date
    });

这将返回我的所有关联记录,我如何最好只选择记录 #10 到 #20,这样我就不会加载任何冗余数据?

    var q = (from Comments in db.tblBlogComments where Comments.blogID == this.ID orderby Comments.date descending select new {
        Comments.userID, Comments.comment, Comments.date
    });

This returns ALL my associated records, how best would I select say records #10 to #20 only so I don't load any redundant data?

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

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

发布评论

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

评论(3

情魔剑神 2024-10-27 20:27:56

怎么样:

var q = (
from Comments in db.tblBlogComments 
where Comments.blogID == this.ID 
orderby Comments.date descending 
select new { Comments.userID, Comments.comment, Comments.date }).Skip(10).Take(10);

How about:

var q = (
from Comments in db.tblBlogComments 
where Comments.blogID == this.ID 
orderby Comments.date descending 
select new { Comments.userID, Comments.comment, Comments.date }).Skip(10).Take(10);
聽兲甴掵 2024-10-27 20:27:56

您可以在结果集上使用 .Skip().Take() 方法。示例:

var q = (from Comments in db.tblBlogComments where Comments.blogID == this.ID orderby Comments.date descending select new {
    Comments.userID, Comments.comment, Comments.date
});

然后使用:

int pageSize = 10;
int page = 3;
var currentPage = q.Skip((currentPage - 1) * pageSize).Take(pageSize);

然后

foreach(var item in currentPage)
{
    ...
}

由于 Linq 使用延迟执行,将在 foreach 循环期间创建并执行实际查询。所以SQL查询只会返回当前页面的记录。

编辑:有关此主题的更多信息

You can use the .Skip() and .Take() methods on your result set. Example:

var q = (from Comments in db.tblBlogComments where Comments.blogID == this.ID orderby Comments.date descending select new {
    Comments.userID, Comments.comment, Comments.date
});

And then use:

int pageSize = 10;
int page = 3;
var currentPage = q.Skip((currentPage - 1) * pageSize).Take(pageSize);

And then

foreach(var item in currentPage)
{
    ...
}

Since Linq uses deferred execution, the actual query will be created and executed during the foreach loop. So the SQL query will only return the records for the current page.

Edit: More information about this subject

饮湿 2024-10-27 20:27:56
int start = 10;
int end = 20;
var q = (from Comments in db.tblBlogComments 
            where Comments.blogID == this.ID 
            orderby Comments.date descending 
            select new {
                          Comments.userID, 
                          Comments.comment, 
                          Comments.date
                       }).Skip(start).Take(end - start);

我不确定 Skip 是否会转换为在数据库中执行的 SQL,因此这可能不是那么有效。

int start = 10;
int end = 20;
var q = (from Comments in db.tblBlogComments 
            where Comments.blogID == this.ID 
            orderby Comments.date descending 
            select new {
                          Comments.userID, 
                          Comments.comment, 
                          Comments.date
                       }).Skip(start).Take(end - start);

I'm not sure if Skip translates to SQL executed in the database, so this might be not so efficient.

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