c# - 使用LINQ从表的10个字段中选择3个字段

标签 c# .net linq-to-entities compiler-errors

我的查询:

public List<Book> GetAllBook()
{
    return (from c in this.LDEntities.Book
             select new
             {
                 c.IdBook,
                 c.NameBook,
                 c.Athour
             }).ToList();
}

错误:

Cannot implicitly convert type 'AnonymousType#1' to 'System.Collections.Generic.List



什么是正确的代码?

更新:我从Entity Framework in Layered Architectures使用。

最佳答案

尝试这个:

public List<Book> GetAllBook() {     

    var q = (from c in this.LDEntities.Book              
             select new Book() 
             { 
                 IdBook = c.IdBook, 
                 NameBook = c.NameBook, 
                 Athour = c.Athour }).ToList();     
    return (q); 
} 

换句话说,创建Book类型的集合,而不是匿名类型的集合。

关于c# - 使用LINQ从表的10个字段中选择3个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7167753/

相关文章:

c# - 使用 ConfigureServiceInIsolation - Generic Host 运行 Topshelf 服务

c# - 来自 OnEventActivityAsync 的数据在第一次之后不会更改 ConversationState

c# - 匿名类型的非只读替代

c# - Nullable<int> 是 "Predefined value type"- 或者 Equals() 和 == 在这里如何工作?

c# - 从 EF 选择返回元组

c# - 动态添加可点击列表项到 Windows Phone ListBox

.net - 找不到ConfigurationManager

c# - 控制大小在我构建时自行更改。 C#.NET WinForm DevExpress

c# - 获取以下实体之间的差异 Linq to Entities

c# - 如何在不传入连接名称的情况下在 Entities ObjectContext 上设置 EF Trace