LINQ to Entities Any() and Contains() slow with small list

前端 未结 2 747
借酒劲吻你
借酒劲吻你 2021-01-22 08:37

I\'m using EF 6 to get products from a database. The product categories are mapped as a navigation property on products, and the data is from a ProductCategory pivot table. The

2条回答
  •  盖世英雄少女心
    2021-01-22 08:45

    Try first filtering out the products and only then forming your models (Product_PL and Category_PL):

    var filteredProducts = db.Products.Where(p => p.Categories.Any(c => currentCategoryIdAndChildren.Contains(c.ID)))
        .Select(p => new Product_PL
        {
            id = p.ID,
            name = p.Name,
            description = p.Description,
            categories = p.Categories
                        .Select(c => new Category_PL
                        {
                            categoryid = c.ID,
                        }),
        });
    

提交回复
热议问题