我有这两个模型:
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; }
}
一些Products 通过属性组合在一起ProductGroupId,我希望能够Product从ProductInCategory单个 Db 查询中删除整组s 。
控制器方法接收 aproduct_id和 a category_id,而不是 a ProductGroupId。
对于单个Product我一直在使用此查询将其从类别中删除:
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()-bit上失败了:
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();
摇曳的蔷薇
九州编程
相关分类