0

I'm having trouble understanding how to group data in ViewModel, fill it and pass it to the view. I use Entity Framework with a code-first approach.

My domain model is:

public class Product
{
        public int ProductId { get; set; }

        [DisplayName("Name of the product")]
        public string ProductName { get; set; }

        [DisplayName("Descritpion of the product")]
        public string ProductDescription { get; set; }

        [DisplayName("Price of the product")]
        public decimal ProductPrice { get; set; }

        public int EAN { get; set; }

        public int CategoryId { get; set; }

        public virtual Category Categories { get; set; }
}

I also have my view model:

public class ProductIndexGroup
{
    public int EAN { get; set; }
    public string ProductName { get; set; }
    public int Quantity { get; set; }
}

What I am aiming for is fairly simple, but as a newbie in ASP.NET MVC, I'm having trouble achieving it. I would like to group data using my view model and display it in view like this:

EAN      Product Name     Quantity
-----------------------------------
12354    Product1         5
98765    Product2         2

Any help is greatly appreciated.

4
  • So in your data model every product has an entry for each item in inventory? Wouldn't it make more sense to have a quantity field in the product table? For instance if you have 1000 if a particular product, in your data model you will have 1000 product entries with the same data. Commented Dec 10, 2018 at 17:43
  • @bhmahler Yes every product has its own entry. Could you please elaborate on this a little more? Commented Dec 10, 2018 at 18:03
  • 2
    Rather than storing an entry for the quantity, simply add a quantity column to your product table. Then you can just store how many you have rather than a duplicated entry for each one you have. Assuming all of the fields other than the ProductID PK are identical for all entries. Commented Dec 10, 2018 at 18:04
  • Yes that sounds better than what I have. Thanks for the advice Commented Dec 10, 2018 at 18:37

1 Answer 1

3

You can get the quantities, assuming that you have 1 record for each instance of a product using a groupBy query.

Something to note is that this is not the most ideal data structure as you will have repeated records for all of your quantities. It will become very inefficient to store thousands of duplicate records to keep track of quantity.

var products = productCollection //This is however you are accessing your product db set 
                   .GroupBy(c => new { c.EAN, c.ProductName})
                   .Select(c => new ProductViewModel { 
                       EAN = c.Key.EAN, 
                       ProductName = c.Key.ProductName, 
                       Quantity = c.Count()
                    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.