下面我提供解释问题的简单示例: 在db我有表书
Id标题年份作者
1 WarAndPease 1800 Tolstoy
2 Hobbit 1880 Tolkien
在代码中我添加了类
public class BookDto
{
public string Title {get;set;}
public string Author{get;set;}
}
然后我创建bookDtos列表
IEnumerable<BookDto> booksInMemory = new List<BookDto>
{
new BookDto{Title = "SomeTitle1",Author = "Author1"},
new BookDto{Title = "SomeTitle2",Author = "Author2"},
}
然后我从db查询书籍并映射到bookdto
IQueryable<BookDto> booksInDb = context.Books.Select(b => new BookDto
{
Title = b.Title,
Author = b.Author
})
我想将内存中的书籍与数据库中数据库中的书籍联合起来
var allBooks = booksInDb.union(booksInMemory)
编写这段代码我想要实体框架pruduce sql
select Books.Title,Books.Author from Books
union
select SomeTitle1 as Title,Author1 as Author
union
select SomeTitle2 as Title,Author2 as Author
但是当我执行toList时,我得到异常无法创建类型的常量值。
注意:我知道为什么抛出异常,我已经阅读了很多信息。问题是为什么实体框架不能简单地生成sql。