代码之家  ›  专栏  ›  技术社区  ›  Stian

如何根据不同对象类型的另一个列表从数据库中获取列表?

  •  1
  • Stian  · 技术社区  · 7 年前

    我有这两种型号:

    public class Product
    {
        public int Id { get; set; }
        public int ProductGroupId { get; set; }
        public int ProductGroupSortOrder { get; set; }
        // ... some more properties
        public ICollection<ProductInCategory> InCategories { get; set; }
    }
    
    public class ProductInCategory
    {
        public int Id { get; set; }
        public int ProductId { get; set; }
        public int ProductCategoryId { get; set; }
        public int SortOrder { get; set; }
    
        // Nav.props.:
        public Product Product { get; set; }
        public ProductCategory ProductCategory { get; set; }
    }
    

    一些 Product s通过属性分组在一起 ProductGroupId 我希望能够删除 产品 来自于 ProductInCategory 在单个数据库查询中。

    控制器方法接收 product_id 和A category_id ,不是 产品组ID .

    单人间的 产品 我已使用此查询将其从类别中删除:

    ProductInCategory unCategorize = await _context.ProductsInCategories
        .Where(pic => pic.ProductId == product_id && pic.ProductCategoryId == category_id)
        .FirstOrDefaultAsync();
    

    然后:

    _context.Remove(unCategorize);
    await _context.SaveChangesAsync();
    

    现在,如果我有 List<Product> 我想从中删除的 ProductsInCategories ,查询将是什么样的?

    我试过了,但是失败了 .Any() -位:

    Product product = await _context.Products
        .Where(p => p.Id == product_id)
        .FirstOrDefaultAsync();
    
    List<Product> products = await _context.Products
        .Where(g => g.ProductGroupId == product.ProductGroupId)
        .ToListAsync();
    
    List<ProductInCategory> unCategorize = await _context.ProductsInCategories
        .Where(pic => pic.ProductId == products.Any(p => p.Id)
            && pic.ProductCategoryId == category_id)
        .ToListAsync();
    
    4 回复  |  直到 7 年前
        1
  •  2
  •   Ivan Stoev    7 年前

    product_id category_id ProductGroupId

    int? productGroupId = await _context.Products
        .Where(p => p.Id == product_id)
        .Select(p => (int?)p.ProductGroupId)
        .FirstOrDefaultAsync();
    
    if (productGroupId == null)
    {
        // Handle non existing product_id 
    }
    

    Product

    List<ProductInCategory> unCategorize = await _context.ProductsInCategories
        .Where(pic => pic.Product.ProductGroupId == productGroupId)
        .ToListAsync();
    
        2
  •  1
  •   Ajay Gupta    7 年前

    List<ProductInCategory> unCategorize = await _context.ProductsInCategories
        .Where(pic => products.Select(p => p.Id).Contains(pic.ProductId)
            && pic.ProductCategoryId == secondary_id)
        .ToListAsync();
    
        3
  •  1
  •   Flater    7 年前

    DELETE FROM People WHERE Name = 'Bob' 
    

    context.Database.ExecuteSqlCommand(
            "DELETE FROM Products WHERE ProductGroupId = " + product.ProductGroupId 
          ); 
    

    product.ProductGroupId

    Entity Framework Extensions

    context.Customers.Where(x => x.ID == userId).DeleteFromQuery();
    

    _context.Products.Where(g => g.ProductGroupId == product.ProductGroupId).DeleteFromQuery();
    


        4
  •  0
  •   Sachila Ranawaka    7 年前

    products

    .Where(pic => products && pic.ProductId == products.Any(p => p.Id)
        && pic.ProductCategoryId == secondary_id)
    
    推荐文章