C# 其中-按值列表选择

C# 其中-按值列表选择,c#,asp.net-core,C#,Asp.net Core,有两种模式 类别: public class Categories { public int CategoriesId { get; set; } public int? StoroniyId { get; set; } public string Name { get; set; } public int? Parent_id { get; set; } public ICollection<Products> Produkt { get;

有两种模式 类别:

public class Categories
{
    public int CategoriesId { get; set; }
    public int? StoroniyId { get; set; }
    public string Name { get; set; }
    public int? Parent_id { get; set; }
    public ICollection<Products> Produkt { get; set; }
}
无论如何,都不可能根据父类别选择商品:

public async Task<IActionResult> Index()
{
    var CatId = await _context.Categories.Where(c => c.Parent_id == 3).ToListAsync();
    var applicationDbContext = _context.Products.Where(c => CatId.Equals(c.CategoriesId));
    return View(await applicationDbContext.ToListAsync());
}
公共异步任务索引()
{
var CatId=await_context.Categories.Where(c=>c.Parent_id==3.tolistSync();
var applicationDbContext=_context.Products.Where(c=>CatId.Equals(c.CategoriesId));
返回视图(等待applicationDbContext.toListSync());
}

如何正确编写运算符,其中

您应该将相应的行更改为下一行

var applicationDbContext = _context.Products.Where(c => CatId.Any(cat => cat.CategoriesId == c.CategoriesId));
您在第一个
where
子句中获得了一个类别列表,因此您必须检查产品是否属于所有此类类别

不过我会做得有点不同

var products = from c in _context.Categories
               from p in _context.Products
               where c.CategoriesId == p.CategoriesId && c.Parent_id == 3
               select p;

return View(await products.ToListAsync());
var products = from c in _context.Categories
               from p in _context.Products
               where c.CategoriesId == p.CategoriesId && c.Parent_id == 3
               select p;

return View(await products.ToListAsync());