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 8513927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:43:14+00:00 2026-06-11T04:43:14+00:00

ProductCategory = "A" Public Class ProdCat { public int productId, public string productCategory }

  • 0
 ProductCategory = "A"
 Public Class ProdCat
 {
     public   int productId,
     public string  productCategory
}

I have a list like this

List<ProdCat> ProdCatList = new List<ProdCat>();
ProdCatList.Add( new ProdCat{ productId= 1, productCategory= "A" } );
ProdCatList.Add( new ProdCat{ productId= 2, productCategory= "B" } );
ProdCatList.Add( new ProdCat{ productId= 3, productCategory= "A" } );
ProdCatList.Add( new ProdCat{ productId= 4, productCategory= "C" } );
ProdCatList.Add( new ProdCat{ productId= 5, productCategory = "A" } );
ProdCatList.Add( new ProdCat{ productId = 6, productCategory= "B" } );

I need to come up with a list like this. Please note ProductCatList is a list and it has two properties, Namely Product Category which is a sting and productIds which is another list in ProductCatList

ProductCatList
     ProductCategory = "A"
         productId = 1
         productId = 3
         productId = 5
   ProductCategory = "B"
         productId = 2
         productId = 6
   ProductCategory = "C"
         productId = 4

Class Implementation would be like below

Public Class ProductCatGroup
{
   string      ProductCategory ,
   IList <int> ProdcutId 

}

How can i achieve something like above using LINQ


I have two classes as below

 public class product
            {
    
                public int productId { get; set; }
                public string productName { get; set; }
            }
    
            public class ProductCat
            {
    
    
                public int productId { get; set; }
                public string productName { get; set; }
                public int productCatId { get; set; }
                public string ProductCatName { get; set; }
            }
    

The list is as below

   List<ProdCat> ProdCatList = new List<ProdCat>();
    ProdCatList.Add( new ProdCat{ productId= 1, productCatId = "1", productName="PA", ProductCatName = "PC1"});
    ProdCatList.Add( new ProdCat{ productId= 2, productCatId = "2", productName="PB", ProductCatName = "PC2"});
    ProdCatList.Add( new ProdCat{ productId= 3, productCatId = "2", productName="PC", ProductCatName = "PC2"});
    ProdCatList.Add( new ProdCat{ productId= 4, productCatId = "1", productName="PD", ProductCatName = "PC1"});
    ProdCatList.Add( new ProdCat{ productId= 5, productCatId = "1", productName="PE", ProductCatName = "PC1"});

And I need to come up with something like below

 ProductCatGroup
      productCatId = "1"
      ProductCatName = "PC1"
           productId = 1
           productName = "PA"
           productId = 4
           productName = "PD"
           productId = 5
           productName = "PE"
      productCatId = "2"
      ProductCatName = "PC2"
             productId = 2
             productName = "PB"
             productId = 3
             productName = "PC"

Class implementation is as below

public class ProductCatGroup
{
           public int productCatId { get; set; }
            public int productCatName{ get; set; }
            public IList<Product> ProductList = new List<Product>();

}

I think this is much clear now

  • 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-11T04:43:15+00:00Added an answer on June 11, 2026 at 4:43 am
    var grouped = ProductCatList.GroupBy(g => new{g.productCatId,g.productCatName})
                         .Select(g => new ProductCatGroup()
                                      {
                                          productCatId = g.Key.productCatId,
                                          productCatName = g.Key.productCatName,
                                          ProductList = g.Select(p => new Prroduct()
                                                        {
                                                             productid = p.productId,
                                                             productName = p.productName
                                                         }
                                      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class in my project public class ProductCategory { public virtual
I have a class called Product with this structure: public class Product { public
My XML looks like this <page> <orderhistory> <order> <ordernr>10610000000001</ordernr> <orderrecord> <productcategory>Something</productcategory> </orderrecord> <orderrecord> <productcategory>Something</productcategory>
I have two very simple tables. Product and ProductCategory (ProductCategory is like a 'lookup'
I have 2 tables which have many-to-many relation. Code of entities public class Product
This is my Insert Statement INSERT INTO ProductStore (ProductID, StoreID, CreatedOn) (SELECT DISTINCT(ProductId), 1,
I have the following method. This works fine if i remove the following line
Let's say I have a product class that has a Category property on it.
I Have 3 Relational Tables on SQLCE Database. Product       ProductCategory     ProductPhoto ----------
Suppose the following model : class Product include MongoMapper::Document key :name, String key :product_category_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.