跳过实体框架核心 [英] Skip and Take in Entity Framework Core

查看:57
本文介绍了跳过实体框架核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的POCO类:

I have simple POCO classes:

public class Library
{
    [Key]
    public string LibraryId { get; set; }

    public string Name { get; set; }

    public List<Book> Books { get; set; }
}

public class Book
{
    [Key]
    public string BookId { get; set; }

    public string Name { get; set; }

    public string Text { get; set; }
}

我有一个查询,它返回已经包含书籍的图书馆:

And I have query, that returns libraries with already included books:

dbContext.Set<Library>.Include(x => x.Books);

我正尝试跳过5个库,然后再使用10个:

I'm trying to skip 5 libraries and then take 10 of them:

await dbContext.Set<Library>.Include(x => x.Books).Skip(5).Take(10).ToListAsync();

问题是,当我尝试执行 Skip Take 方法,它将返回不包含书本的图书馆。

The problem is, that when I'm trying to perform Skip and Take methods on this query, it returns libraries without included list of books.

如何可以使用跳过接受并保存以前包含的实体吗?

How can I work with Skip and Take, with saving previously included entities?

推荐答案

通常,在使用 Skip Take 方法。尝试按这种方式按名称排序:

Usually you need to Order By first before use Skip and Take methods. Try ordering by name like this way:

await dbContext.Set<Library>().Include(x => x.Books)
                              .OrderBy(x=>x.Name)
                              .Skip(5)
                              .Take(10)
                              .ToListAsync();

据我记得,您的查询应使用 OFFSET-FETCH 过滤器,该过滤器要求存在 ORDER BY 子句。

As far as I remember your query should be translated using OFFSET-FETCH filter which requires an ORDER BY clause to exist.

这篇关于跳过实体框架核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆