【发布时间】:2019-11-08 11:17:59
【问题描述】:
我有一个书籍数据库,其中包含作者的 ICollection。我想使用 LINQ 根据 AuthorId 返回作者对象。
Book db
int BookId
string Name { get; set; }
public ICollection<Author> Authors { get; set; }
Author db
int AuthorId
string Name
ICollection<Quote> Quotes { get; set; }
ICollection<Penname> Pennames { get; set; } - Edit: Added for clarity
我试过了:
var test = _context.Book.Include(x => x.Authors).Include("Authors.Quotes")
.Select(y => y.Authors)
这给了我:
EntityQueryable<ICollection<Authors>>
[0] {HashSet<Author>} [0]{Author} [1]{Author} [3]{Author}
[1] {HashSet<Author>} [0]{Author} [1]{Author}
[2] {HashSet<Author>} [0]{Author} [1]{Author}
我只是不知道如何迭代作者列表中的作者。类似于以下内容:
var id = 2
var test = _context.Book.Include(x => x.Authors).Include("Authors.Quotes")
.Select(y => y.Authors.Select(x => x.Author).Where(x => x.AuthorId == id))
如果我进行重大更新,我可能会使用弹性...
更新@Marko Papic:
谢谢。奇怪的是,如果我使用下面的内容来获取作者的书籍列表,我会得到我期望的引号和笔名列表
var test = _context.Book.Include(x => x.Authors)
.ThenInclude(x => x.Quotes)
.Include(x => x.Authors)
.ThenInclude(x => x.Pennames)
但是,如果我使用 SelectMany,那么引号和笔名最终为 null
var test = _context.Book.Include(x => x.Authors)
.ThenInclude(x => x.Quotes)
.Include(x => x.Authors)
.ThenInclude(x => x.Pennames)
.SelectMany(x => x.Authors).Where(x => x.AuthorId == id);
Author myauthor
int AuthorId = 2
string Name = "Bob"
ICollection<Quote> Quotes = null
ICollection<Penname> Pennames = null
【问题讨论】:
-
你用的是什么版本的EF?
-
我运行了 dotnet ef --version 并获得了 Entity Framework Core .NET 命令行工具 2.2.4-servicing-10062。这是 ASP.NET Core 2.2
-
如果您想返回带有特定
AuthorId的Author对象,为什么还要使用Books?只需使用_context.Authors.First(a => a.AuthorId == AuthorId)。 -
如果您要过滤附加到
Book的Authors集合,则不能这样做。例如,请参阅this question。 -
谢谢,我会调查一下
标签: asp.net linq entity-framework-core