public class Book
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; } [Required, MaxLength()]
public string Title { get; set; }
public Double? Price { get; set; }
}
解决方案一:
var bkList = db.Books.Select(allBooks => new { Id = allBooks.Id, Title = allBooks.Title }).Select(b => new Book { Id=b.Id,Title=b.Title});
解决方案二:
var bkList2 = db.Books.Select(allBooks => new { Id = allBooks.Id, Title = allBooks.Title }).ToList().ConvertAll<Book>(b => new Book(){ Id=b.Id,Title=b.Title});
解决方案三:
//声明一个与Book一模一样的克隆类
public class BookClone
{
public int Id { get; set; } public string Title { get; set; } public Double Price { get; set; }
} var bkList3 = db.Books.Select(allBooks => new BookClone { Id = allBooks.Id, Title = allBooks.Title });
这三种方案都可以解决The entity or complex type '{0}' cannot be constructed in a LINQ to Entities query. 的异常