位置 - 按值列表

时间:2017-03-25 09:25:47

标签: 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; set; }
}

model Produkt

public class Products
    {
        public int ProductsId { get; set; }
        public string Name { get; set; }

        public int CategoriesId { get; set; }
        public virtual Categories Categoria { get; set; }
        public int Possition { get; set; }
        public bool Glass { get; set; }
        public decimal price_opt { get; set; }
        public decimal price_rrc { 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());
}

如何正确编写运算符

1 个答案:

答案 0 :(得分:0)

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

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());