1

I have a list of type ProductDetailDTO.

List<ProductDetailDTO> productDTOs;

public class ProductDetailDTO
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public byte[] Image { get; set; }
    public string Description { get; set; }
    public string Brand { get; set; }
    public string GUID { get; set; }
    public string VariantName { get; set; }
    public string VariantValue { get; set; }
    public decimal Price { get; set; }
}

I have used linq to bind the data to the list.

var productDetails = (from product in ekartEntities.Products
                                  join productImage in ekartEntities.ProductImages on product.ProductId equals productImage.ProductId
                                  join category in ekartEntities.ProductCategories on product.Category equals category.CategoryId
                                  join mapping in ekartEntities.ProductVariantMappings on product.ProductId equals mapping.ProductId
                                  join variant in ekartEntities.ProductVariants on mapping.ProductVariantId equals variant.ProductVariantId
                                  join inventory in ekartEntities.Inventories on mapping.GUID equals inventory.Guid
                                  where product.ProductId == productDetailDTO.ProductId
                                  select new ProductDetailDTO()
                                  {
                                      ProductId = product.ProductId,
                                      Name = product.Name,
                                      Category = category.Name,
                                      Description = product.Description,
                                      Brand = product.Brand,
                                      Image = productImage.Image,
                                      GUID = mapping.GUID.ToString(),
                                      VariantName = variant.Name,
                                      VariantValue = mapping.Value,
                                      Price = inventory.Price
                                  }).ToList();

Now, I want to display all the variants (VariantName and VariantValue) with the same GUIDs together. How can I achieve that?

Suyash Gupta
  • 546
  • 10
  • 31

2 Answers2

2

You can use GroupBy and Select like this:

 var variants = productDTOs
                   .GroupBy(k => k.GUID)
                   .Select(v => v
                         .Select(variant => new 
                         {
                             variant.VariantName, 
                             variant.VariantValue
                         }));
Bizhan
  • 16,157
  • 9
  • 63
  • 101
-1

You can use Group By

group p by p.GUID into g
select new { Id = g.Key, ProductDetail = g.ToList()).ToList();

If you have tables before group by, then you can add new object in the group itself

group new { p.xyz, n.xyz }
by new { p.GUID } into g

otherwise use let to save the intermediate object in an object and do grouping on it

ManishM
  • 583
  • 5
  • 7