locked
Confused: How do I Get the count of each product ??? RRS feed

  • Question

  • User-973886032 posted

    hi guys,

    I have a hierarchy of categories to the product listing. 

    How do I get the count of each product in my code below

    something like this 

    products = u.Department_Category_Registration.Products.Count();  (For each unique product e.g. TShirts(12 in stock) , Trousers (200 in stock) , shoes (100 in stock)

    which has been commented out in the code below

    thanks

    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());
    Wednesday, July 10, 2019 10:26 PM

Answers

  • User711641945 posted

    Hi afrika,

    If you want to get the count ,you need a property of ProductCount to show the data in the view. And the relationship between Departments_SubCategory_Registration and Products is one-to-many.

    Here is a working demo:

    public class Departments_SubCategory_Registration
        {
            [Key]
            public int SubCategoryID { get; set; }              // This is the PK
            public int CategoryID { get; set; } // this is a FK
           // [ForeignKey("SubCategoryID")]
            public Departments_Category_Registration Departments_Category_Registration { 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 Guid UniqueId { get; set; }
            public bool IsEnabled { get; set; }
            public List<Product> Products { get; set; }
            public int ProductCount { get; set; }
        }
     [Route("/{C}")]
            public async Task<IActionResult> Product(String C)
            {
                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,
                          ProductCount=u.Products.Count()
                      }).ToListAsync()) ; 
            }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 11, 2019 8:17 AM

All replies

  • User711641945 posted

    Hi afika,

    products = u.Department_Category_Registration.Products.Count();  (For each unique product e.g. TShirts(12 in stock) , Trousers (200 in stock) , shoes (100 in stock)

    1. There is a property of products in Departments_SubCategory_Registration and a property of Products in Department_Category_Registration .Is it a Navigation property?

    2.Do you means you have a Products class, and it has many data like below ,your aim is to get the count of each product:

    id name
    1 TShirts
    2 TShirts
    3 Trousers

    Could you share your models that related to your action which could enable me to reproduce your problem better?

    Best Regards,

    Rena

    Thursday, July 11, 2019 5:25 AM
  • User-973886032 posted

    Something like this in red

    id name 
    1 TShirts (245 in stock)
    2 TShirts ( 170 in stock)
    3 Trousers ( 400 in stock)

     here are my models

        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; }
    
        }

    Thursday, July 11, 2019 7:24 AM
  • User711641945 posted

    Hi afrika,

    If you want to get the count ,you need a property of ProductCount to show the data in the view. And the relationship between Departments_SubCategory_Registration and Products is one-to-many.

    Here is a working demo:

    public class Departments_SubCategory_Registration
        {
            [Key]
            public int SubCategoryID { get; set; }              // This is the PK
            public int CategoryID { get; set; } // this is a FK
           // [ForeignKey("SubCategoryID")]
            public Departments_Category_Registration Departments_Category_Registration { 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 Guid UniqueId { get; set; }
            public bool IsEnabled { get; set; }
            public List<Product> Products { get; set; }
            public int ProductCount { get; set; }
        }
     [Route("/{C}")]
            public async Task<IActionResult> Product(String C)
            {
                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,
                          ProductCount=u.Products.Count()
                      }).ToListAsync()) ; 
            }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 11, 2019 8:17 AM
  • User-973886032 posted

    thanks, but the Product count is ( @Html.DisplayFor(modelItem => item.ProductCount)) result is 0 in my view

    Thursday, July 11, 2019 9:06 AM
  • User1034446946 posted

    Add an include for the products

    Thursday, July 11, 2019 2:43 PM
  • User711641945 posted

    Hi afrika,

    I test the project by using your code and it works well.

    afrika

    but the Product count is ( @Html.DisplayFor(modelItem => item.ProductCount)) result is 0 in my view

    You mean that you get the record but don't have data in ProductCount ? If so, I suggest that you need check your database.

    Best Regards,

    Rena

    Friday, July 12, 2019 2:00 AM
  • User-973886032 posted

    Rena Ni

    Hi afrika,

    If you want to get the count ,you need a property of ProductCount to show the data in the view. And the relationship between Departments_SubCategory_Registration and Products is one-to-many.

    Here is a working demo:

    public class Departments_SubCategory_Registration
        {
            [Key]
            public int SubCategoryID { get; set; }              // This is the PK
            public int CategoryID { get; set; } // this is a FK
           // [ForeignKey("SubCategoryID")]
            public Departments_Category_Registration Departments_Category_Registration { 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 Guid UniqueId { get; set; }
            public bool IsEnabled { get; set; }
            public List<Product> Products { get; set; }
            public int ProductCount { get; set; }
        }
     [Route("/{C}")]
            public async Task<IActionResult> Product(String C)
            {
                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,
                          ProductCount=u.Products.Count()
                      }).ToListAsync()) ; 
            }

    Best Regards,

    Rena

    I got it to work, as you had commented out the FK line (which I highlighted in red above) in my model, so it was returning 0. But when I un-comment it, it works

    many thanks

    Friday, July 12, 2019 9:57 AM