我有Book和Author域类,以及BookDto和AuthorDto类,它们是各自域类的子选。
在应用程序中的某个地方,我必须公开一个IQueryable<BookDto>而不是IQueryable<Book>,所以,我编写了以下代码来进行转换,没有什么复杂的:
var queryableDto = dbContext.Books.Select(book => new BookDto
{
Name = book.Name,
...
Author = book.Author == null ? null : new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
}
});然后我做了这个:
var dtos = queryableDto.Select(bDto => new BookDto
{
Name = bDto.Name,
Author = bDto.Author == null ? null : new AuthorDto() { Name = bDto.Author.Name }
}).ToList();但是在我的第二个查询中,null-check有一个很大的问题(它显示为Author = bDto.Author == null ? null : new...)。因为当我说:bDto.Author == null时,发生的是实体框架从数据库中检索整个Author对象及其所有列,但显然不应该是这种情况,也不是我想要做的。当您使用域类的IQueryable时(如在第一个代码块中),这些空检查将被转换为SQL语句,但当您执行类似操作时则不会。
我也无法摆脱null-check,因为我必须找出这本书是否有作者。(我知道在现实生活中,不可能有一本书没有作者,但这只是一个例子)
那么,真的有办法解决这个问题吗?我如何告诉实体框架,当我说bDto.Author == null时,检索整个东西并不是我真正想要做的事情?
发布于 2020-10-01 15:06:20
所以在作者和书籍之间有一个一对多的关系:每个作者写了零本或多本书,每本书只有一个作者,即外键AuthorId引用的作者。
我使用完整的实体框架。对于您的问题,我将使用这本书的虚拟属性。实体框架知道作者和图书之间的关系,并将为您创建正确的(Group-)join。
我听说有传言说实体框架的核心版本在使用虚拟属性时有一些限制,尽管我从来没有检查过它们。因此,我将为您提供使用虚拟属性的解决方案,以及您自己进行连接的解决方案。
使用虚拟特性。
如果您遵循了entity framework conventions,您的类将类似于以下内容:
class Author
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Author wrote zero or more Books (one-to-many)
public virtual ICollection<Book> Books {get; set;}
}
class Book
{
public int Id {get; set;}
public string Name {get; set;}
...
// Every Book has been written by exactly one Author, using foreign key
public int AuthorId {get; set;}
public virtual Author Author {get; set; }
}解决方案可能如下所示:
public MyDbContext : DbContext
{
public DbSet<Author> Authors {get; set;}
public DbSet<Book> Books {get; set;}
public IQueryable<BookDto> BookDtos => this.Books.Select(book => new BookDto
{
Id = book.Id,
Name = book.Name,
...
// A BookDto has exactly one Author
Author = new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
},
});
}如果您想强制使用BookDtos而不是Books,可以将查询包装在一个接口中,并提供该接口而不是完整的MyDbContext。
你自己加入吧
IQueryable<BookDto> BookDtos => this.Books.Join(
this.Authors,
book => book.AuthorId, // from every book take the foreign key to Author
author => author.BookId, // from every Author take the primary key
// parameter resultSelector: for every Book with its one-and-only Author,
// make one new BookDto:
(book, author) => new BookDto
{
Id = book.Id,
Name = book.Name,
...
Author = new AuthorDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
},
});如果你想让作者带着他们的书:
IQueryable<AuthorDto> AuthorDtos => this.Authors.GroupJoin(
this.Books,
author = author.Id, // from every Author take the primary key
book = book.AuthorId, // from every Book take the foreign key to Author
// parameter resulSelector: take every author with his zero or more Books
// to create one new AuthorDto:
(author, booksOfThisAuthor) => new AuthorDto
{
Id = author.Id,
Name = author.Name,
...
Books = booksOfThisAuthor.Select(book => new BookDto
{
Id = book.Author.Id,
Name = book.Author.Name,
...
}),
});在一对多关系中,如果您希望“具有零个或多个子项的项”使用GroupJoin并从单边开始,则使用
。如果您想要“具有外键引用的唯一项的项”,请使用Join并从多个端开始。
如果检查overload of Queryable.GroupJoin with parameter resultSelector,您将看到booksOfThisAuthor的类型为System.Collections.Generic.IEnumerable<TInner>,TResult>>。所以它不是IQueryable<...>。因此,如果你期望用户不想要所有的书,或者不想要完整的BookDto,我不确定这种方法是否有效。
https://stackoverflow.com/questions/64072940
复制相似问题