【问题标题】:Query over ICollection of an entity in in linq to entity在 linq to entity 中查询实体的 ICollection
【发布时间】: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??
  • 是的,我没有注意到这一点

标签: c# linq entity-framework


【解决方案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...,我不知道技术原因
猜你喜欢
  • 2017-08-15
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2011-04-27
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多