-1

a have a list that show the following values

{day:1 , product:1 , value :1} , 
{day:1 , product:2 , value :2}
{day:1 , product:3 , value :3}
.
.
.

i would like to have a list

{day:1 , details:{ {product:1 , value :1} , {product:2 , value :2},{ 
                    product:3 , value :3}
                  }
 },
{day:2 , details:{ {product:1 , value :10} , {product:2 , value :20},{ 
                    product:3 , value :30}
                  }
 },

can anyone help me to do it ?? thanx.

Andrei
  • 55,890
  • 9
  • 87
  • 108

2 Answers2

0

You can do it with anonymous type using linq

var listNew = listBase.Select(x => new
            {
                day= x.day,
                details= new
                {
                    product= x.product,
                    value = x.value
                }
            }).ToList();
0

You can use GroupBy and Anonymous type for achieving this.

var result = list.GroupBy(x=>x.day)
                 .Select(x=> new 
                 {
                    day=x.Key,
                    detail = x.ToList().Select(c=> 
                                      new 
                                      {
                                            product=c.product, 
                                            value=c.value
                                      })
                  });
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51