阿修--一直在自我修炼,希望能得到百年道行

湖南微软开发者俱乐部

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
public class Book
{
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required, MaxLength(50)]
        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. 的异常

posted on 2013-08-12 10:31  阿修  阅读(1790)  评论(0编辑  收藏  举报