0

I would like to split my array that consists of objects in arrays, grouping them by some property of the type (let's say

string Group {get; set;}

).

At the beginning I have

IEnumerable<T> array.

Then I would like to get

IEnumerable<IEnumerable<T>> array.

Of course, I can do it without LINQ, but it will look too ugly and verbose.

1
  • 1
    "Of course, I can do it without LINQ, but it will look too ugly", why don't you show us how ugly that might look? :-) Commented Nov 25, 2016 at 23:34

1 Answer 1

2
array.GroupBy(item => item.Group)

this will give you IEnumerable<IGrouping<string, YourType>>. The IGrouping<string, YourType> extends IEnumerble<YourType> with Key, which will be the value of Group property in your example:

foreach (var g in array.GroupBy(item => item.Group))
{
    Console.WriteLine("Group='{0}', {1} items", g.Key, g.Count());
}
Sign up to request clarification or add additional context in comments.

1 Comment

beautiful solution.Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.