Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by an array inside an array using Linq

Tags:

linq

c#-4.0

I can use the below code to group by a ID property in an array which works.

var docArray = MyArray;                                                        

var docGroup = docArray.GroupBy(x => x.ID)
    .Select(grp => new
    {
        Id = grp.Key,
        Results = grp.ToList(),
    }).ToList();

If MyArray has another array inside it which has a property say Data can some please tell me how to do the grouping based on the Data property.

class MyArray
{
    SecondArray[] arr = new SecondArray[2];
    public int ID{get;set;}   
}

class SecondArray
{
    public string Data{ get; set; }   
}
like image 514
user505210 Avatar asked May 26 '26 16:05

user505210


1 Answers

var query = from a in docArray
            from b in a.arr
            group new { a, b } by b.Data into g
            select new
            {
              g.Key,
              Results = g.ToList()
            };
like image 72
Aducci Avatar answered May 30 '26 13:05

Aducci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!