Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:51:25+00:00 2026-06-07T16:51:25+00:00

I have the following entity: public class Product{ public int ID { get; set;

  • 0

I have the following entity:

public class Product{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public string SubCategory { get; set; }
}

I would ultimately like to display a page that takes an IEnumerable<Product> and sorts the Products first by Category – then sorts each Category of Products by SubCategory.

What should my ViewModel look like?

At first I came up with something like this:

public class ProductListViewModel{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<string> Categories { get; set; }
    public IEnumerable<string> SubCategories { get; set; }
}

but then my Controller becomes full of logic to extract the unique Categories and SubCategories and I have no clean way of dealing with the fact that each Category has different lists of SubCategories.

Anyone with experience in sorting objects by category – can you nudge me in the right direction of how to approach this situation cleanly? I can get the job done, but I feel I am making my code needlessly sloppy and not DRY.

I am also wondering if my Product class structured in an inappropriate way to handle Categories/SubCategories – does anyone else feel the same? (I am thinking my Categories may be better suited as separate objects entirely)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:51:27+00:00Added an answer on June 7, 2026 at 4:51 pm

    On your question regarding the model structure, I think that Category should definitely
    be a separate entity – your database will be under normalized (the Category should not be repeated multiple times in the database). Perhaps :

    public class Product{
        public int ID { get; set; }
        public string Name { get; set; }
        public Category Category { get; set; }
    }
    
    public class Category{
        public int ID { get; set; }
        public string Name { get; set; }
        public SubCategory SubCategory { get; set; }
    }
    
    
    public class SubCategory{
        public int ID { get; set; }
        public string Name { get; set; }
    }
    

    This way you can separate out the Category from the Product (you could also have several Categories for a single product by having an ICollection in the Product entity). This also creates a relationship between Category and SubCategory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entity public class DocumentHistory { public string Name { get;
I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
I have the following entity: public class User { public int ID {get; set;}
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
I have the following mapping: @Entity @Table(name = Prequalifications) public class Prequalification implements Serializable
I have the following class/entity: public class Product : ISaleable { public void ProcessSale()
Let's say I have the following entity: public class Store { public List<Product> Products
Consider the following Entities. public class Product{ int id; Date effectiveDate; Date expiryDate; Set<Inventory>
I'm using jpa and I have the following entity: @Entity @Table(name=favorites_folders) public class FavoritesFolder
I have the following simple entity: public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.