goul
goul

Reputation: 853

Get The Full List Of Objects In A Dictionary Values

I have this dictonary:

private Dictionary<int, ICar> _ICarsDic;

The object ICar actually contains another list of objects:

public interface ICar 
{
    int carId { get; set; }
    string carName { get; set; }
    List<IBrandsDetails> brandsDetails { get; set; }
}

I would like to write a function that returns me in a single list all the brandsDetails in all my _ICarsDic values.

I am sure LINQ does that (don't want to write an ugly loop) but I'm new to this so would appreciate your help. Thanks!

Upvotes: 0

Views: 95

Answers (3)

Akanksha Gaur
Akanksha Gaur

Reputation: 2696

You can write the linq as

var listOfObjects = _ICarsDic.SelectMany(t => t.Value.brandsDetails).ToList();

Upvotes: 0

BFree
BFree

Reputation: 103780

var brandDetails = _ICarsDic.Values.SelectMany(c => c.brandsDetails);

Upvotes: 1

Habib
Habib

Reputation: 223432

Use Eneumerable.SelectMany

List<IBrandsDetails> list =  _ICarsDic.SelectMany(r => r.Value.brandsDetails)
                                      .ToList();

Upvotes: 3

Related Questions