Is there any elegant way in LINQ to create a collection in a collection of lists based on a property

I have a collection of cars. I want to put it on separate lists by car property.. Lets say that brand.

So, if I have a collection of some Ford, some Chevy, some BMW, etc., I want a separate list for each of these buckets.

sort of:

IEnumberable<Car> myCarCollection = GetCollection();

List<IEnumerable<Car>> buckets = myCarCollection.BucketBy(r=>r.Brand)

is there something like this or do i need to do this "manually" through loops

+3
source share
1 answer
return myCarCollection.GroupBy(r => r.Brand);
+5
source

Source: https://habr.com/ru/post/1756397/


All Articles