user505210
user505210

Reputation: 1402

Group by an array inside an array using Linq

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; }   
}

Upvotes: 3

Views: 758

Answers (1)

Aducci
Aducci

Reputation: 26644

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()
            };

Upvotes: 3

Related Questions