1

I have the following classes

public class Product
{
    public string PNo {get;set;}
    public String GCode {get;set;}
    public IList<Detail> Details {get;set;}
}

public class Detail{
    public string Color {get;set;}
    public string Size {get;set;}
}

And i have the data in an array as below

PNO     GCode   Color   Size Amount
12345   GCC     A       L       1
12345   GCC     V       M       2 
12345   GCC     C       S       3
12345   GCC     D       XL      4
12345   GCC     E       XS      5

How do i get the following output using groupby so that my output could like shown below

Expected output:

{
PNO: 12345,
GCode: GCC,
options:[
            {Color:A, Size:L, Amount: 1},
            {Color:V, Size:M, Amount: 2},
            {Color:C, Size:S, Amount: 3},
            {Color:D, Size:XL, Amount: 4},
            {Color:E, Size:XS, Amount: 5}
        ]
}

Thanks

Mukil Deepthi
  • 6,072
  • 13
  • 71
  • 156
  • are you trying to generate JSON file from a 2D `string[,]` array ? – Slai Aug 11 '16 at 11:01
  • no i am not generating. because i am writing this for a webapi i am showing the output in json. – Mukil Deepthi Aug 11 '16 at 11:03
  • @Mukil - do you have the array and want to populate classes like those you added? It's is unclear if your data was originally the classes and you wanted such output like at the end of your question or if you have the array and want to have it into the classes – Gilad Green Aug 11 '16 at 14:04
  • i have the data and want the groupby data into the classes. Because i am writing a api when i return this classes i get the json output as shown. – Mukil Deepthi Aug 11 '16 at 14:37

2 Answers2

2

Given the contract you defined. I think below code snippet is OK to generate the JSON you want, I use anonymous object a lot, you can replace with you entity class.

 products.GroupBy(product => new
                  {
                      product.PNo,
                      product.GCode
                  })
         .Select(productGroup => new
         {
             productGroup.Key.PNo,
             productGroup.Key.GCode,
             options = productGroup.SelectMany(product => product.Details.Select(detail => new
             {
                 detail.Color,
                 detail.Size,
                 detail.Amount
             }))
         });

Hope it helps.

Chasefornone
  • 747
  • 1
  • 7
  • 15
1

This will get you that expected output:

Query Syntax:

var result = (from item in collection
             group item by new { item.PNO, item.GCode } into grouping
             select new Product
             {
                 PNo = grouping.Key.PNO,
                 GCode = grouping.Key.GCode,
                 Details = grouping.Select(item => new Detail { Color = item.Color, Size = item.Size, Amount = item.Amount }).ToList()
             }).ToList();

Method Syntax:

var result = collection.GroupBy(item => new { item.PNO, item.GCode })
          .Select(grouping => new Product
          {
              PNo = grouping.Key.PNO,
              GCode = grouping.Key.GCode,
              Details = grouping.Select(item =>   new Detail { Color = item.Color, Size = item.Size, Amount = item.Amount }).ToList()
          }).ToList();

Test Data:

List<dynamic> collection = new List<dynamic>()
{
    new { PNO = "12345", GCode = "GCC", Color = "A", Size="L", Amount=1 },
    new { PNO = "12345", GCode = "GCC", Color = "V", Size="M", Amount=2 },
    new { PNO = "12345", GCode = "GCC", Color = "C", Size="S", Amount=3 },
    new { PNO = "12345", GCode = "GCC", Color = "D", Size="XL", Amount=4 },
    new { PNO = "12345", GCode = "GCC", Color = "E", Size="XS", Amount=5 },
};
Gilad Green
  • 36,708
  • 7
  • 61
  • 95