跳过并接受 Entity Framework Core [英] Skip and Take in Entity Framework Core
问题描述
我有简单的 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.
如何使用 Skip
和 Take
保存以前包含的实体?
How can I work with Skip
and Take
, with saving previously included entities?
推荐答案
通常你需要先Order By,然后才能使用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.
这篇关于跳过并接受 Entity Framework Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!