在 linq to entity 中查询实体的 ICollection

Posted

技术标签:

【中文标题】在 linq to entity 中查询实体的 ICollection【英文标题】:Query over ICollection of an entity in in linq to entity 【发布时间】:2014-05-30 17:04:19 【问题描述】:

我以一种简单的方式问我的问题,假设这些类:

public class Product
    
        public int ProductId  get; set; 
        public string ProductName  get; set; 
        public ICollection<Category> Categories  get; set; 
    

    public class Category
    
        public int CategorytId  get; set; 
        public string CategoryName  get; set; 
        public ICollection<Product> Products  get; set; 
    

    public class ProductViewModel
    
        public int ProductId  get; set; 
        public string ProductName  get; set; 
        public ICollection<CategoryViewModel> CategoryViewModel  get; set; 
    

    public class CategoryViewModel
    
        public int CategorytId  get; set; 
        public string CategoryName  get; set; 
    

    public class ProductContext : DbContext
    
        public DbSet<Product> Products  get; set; 
        public DbSet<Category> Categories  get; set; 
    

不用说产品和类别之间会有多对多的关系,现在我只想查询所有产品及其所有类别,因为每个产品都可以有很多类别。为此,我写了这个:

public IEnumerable<ProductViewModel> GetAllProducts()
        
            var _productContext = new ProductContext();

            var query = from product in _productContext.Products
                        join category in _productContext.Categories on product.ProductId equals category.CategorytId
                        select new ProductViewModel
                        
                            ProductId = product.ProductId,
                            ProductName = product.ProductName,
                            CategoryViewModel = product.Categories.Select(c=>new CategoryViewModel
                            
                                CategorytId = c.CategorytId,
                                CategoryName = c.CategoryName,
                            ).ToList()
                        ;
            return query.ToList();
        

但我收到此错误:

LINQ to Entities 无法识别方法 'System.Collections.Generic.List...

我知道可以使用导航属性和 Include 方法进行查询,但是如何使用 Linq 进行查询?

【问题讨论】:

从内部选择中移除 .ToList 语法错误!你不能将一个对象的实例转换为另一个对象的列表 我想你和我都没有注意到 ProductViewModel 中的 ICollection,正因为如此 .ToList() 仅适用于 IEnumerable 而不是 ICollection?? 是的,我没有注意到这一点 【参考方案1】:

ProductViewModel 类中,您需要将ICollection&lt;...&gt; 更改为IEnumerable&lt;...&gt;,然后删除CategoryViewModel 分配上的内部.ToList()

public class ProductViewModel

   ...
    public IEnumerable<CategoryViewModel> CategoryViewModel  get; set; 


 CategoryViewModel = product.Categories.Select(c=>new CategoryViewModel
                            
                                ...
                            )

【讨论】:

请补充一些我们为什么不能使用 ICollection / ToList() 的细节。 @jony89 - LINQ to Entities does not recognize the method System.Collections.Generic.List...,我不知道技术原因

以上是关于在 linq to entity 中查询实体的 ICollection的主要内容,如果未能解决你的问题,请参考以下文章

在 LINQ to Entities 查询中无法构造实体或复杂类型

在实体框架和 Linq to Entities 中使用规范模式和表达式

无法在 linq to entity 查询中构造实体或复杂类型“x”

在 LINQ to Entities 查询中无法构造实体或复杂类型

优化多个 LINQ to Entity Framework 查询

如果不支持包含,您如何在 LINQ to Entities(实体框架)中执行 SQL 样式的“IN”语句?