如何获得数据库中的产品计数?

时间:2019-07-10 22:24:07

标签: asp.net-core

更新: 我正在努力实现这样的目标

id名称

1张领带(库存245张)

2个领带(库存170个)

3条裤子(库存400件)

我对产品列表的类别有一定的了解。

如何在下面的代码中获取每种产品的数量

类似这样

products = u.Department_Category_Registration.Products.Count();  (For each 

独特的产品,例如TShirts(库存12件),裤子(库存200件),鞋子(库存100件)

在下面的代码中已被注释掉

    return View(await _context.Departments_SubCategory_Registration.Include(c =>        c.Departments_Category_Registration)
            .Where(d => d.Departments_Category_Registration.Category_Name.Contains(C))
              .Where(r => r.IsEnabled == true).Select(u => new      Departments_SubCategory_Registration
              {
                  CategoryID = u.CategoryID,
                  SubCategory_Name = u.SubCategory_Name,
                  EntryDate = u.EntryDate,
                  Description_Detailed = u.Description_Detailed,
                  Description_Short = u.Description_Short 

                // e.g. I am trying to get 
                // products = u.Department_Category_Registration.Products.Count();

              }).ToListAsync());

已更新-这是我的模型 1.产品 2. Department_Category_Registration 3. Department_subCategory_Registration

最高层级从3导航到1

public class Products
{
    [Key]
    public int ProductID { get; set; }
    public string Product_Name { get; set; }
    public int SupplierID { get; set; }
    public int SubCategoryID { get; set; }
    [ForeignKey("SubCategoryID")]
    public Departments_SubCategory_Registration Departments_SubCategory_Registration { get; set; }
    public DateTime EntryDate { get; set; }
    public string Product_Code { get; set; }
    public string Description_Short { get; set; }
    public string Description_Long { get; set; }
    public decimal Price_Unit { get; set; }
    public decimal? Price_Advert { get; set; }
    public bool Stock { get; set; }
    public bool IsEnabled { get; set; }

}




public class Departments_SubCategory_Registration
{
    [Key]
    public int SubCategoryID { get; set; }              // This is the PK

  //  [ForeignKey("DepartmentID")]
    public int CategoryID  { get; set; } // this is a FK
    [ForeignKey("CategoryID")]
    public Departments_Category_Registration Departments_Category_Registration { get; set; }


    [ForeignKey("SubCategoryID")]
    public Products Products { get; set; }
    public string SubCategory_Name { get; set; }
    public DateTime EntryDate { get; set; }
    public string Description_Short { get; set; }
    public string Description_Detailed { get; set; }
    public string Notes { get; set; }
   // public string Reference { get; set; }
    public Guid UniqueId { get; set; }
    public bool IsEnabled { get; set; }


}




public class Departments_Category_Registration
{



    [Key]
    public int CategoryID { get; set; }              // This is the PK


    public int DepartmentID { get; set; } // this is a FK

    [ForeignKey("DepartmentID")]
    public Xadosh.Models.Department.Departments Departments { get; set; }

    [ForeignKey("CategoryID")]
    public Departments_SubCategory_Registration Departments_SubCategory_Registrations { get; set; }
    //public List<Departments_SubCategory_Registration> Departments_SubCategory_Registrations { get; set; }
    public string Category_Name { get; set; }
    public DateTime EntryDate { get; set; }
    public string Description { get; set; }
    public string Description_Detail { get; set; }
    public string Notes { get; set; }
    public string Reference { get; set; }
    public Guid UniqueId { get; set; }
    public bool IsEnabled { get; set; }

}

1 个答案:

答案 0 :(得分:0)

您可以创建一个Dto或ViewModel类,它将更好地描述您的数据:

Departments_SubCategory_RegistrationViewModel
{
 public int CategoryID  { get; set; } 
 public string SubCategory_Name { get; set; }
 public DateTime EntryDate { get; set; }
 public string Description_Short { get; set; }
 public string Description_Detailed { get; set; }
 public int ProductsCount { get; set; }
}

然后您应该使用此查询执行所需的操作:

return View(
    await _context.Departments_SubCategory_Registration
        .Include(c => c.Departments_Category_Registration)
        .Where(d => d.Departments_Category_Registration.Category_Name.Contains(C))
        .Where(r => r.IsEnabled == true)
        .Select(u => new Departments_SubCategory_RegistrationViewModel
          {
              CategoryID = u.CategoryID,
              SubCategory_Name = u.SubCategory_Name,
              EntryDate = u.EntryDate,
              Description_Detailed = u.Description_Detailed,
              Description_Short = u.Description_Short 
              ProductsCount = u.Products.Count();
          }).ToListAsync());