Gabriel
Gabriel

Reputation: 969

Linq with a Dictionary inside the class

i have the next class:

public class Example 
{
    String name;
    Dictionary<String, decimal> data;

    public Example() 
    { 
        data = new Dictionary<String, decimal>();
    }
}

Then, using Linq i need to retrieve all distinct String keys in the data field. For example:

e1: 1 - [["a", 2m],["b",3m])

e2: 2 - [["b", 2m],["c",3m])

I'll need a list with: ["a","b","c"]

I hope I was clear enough.

Thanks.

PD: One thing i was missing, i have a List of Examples.

Upvotes: 0

Views: 172

Answers (2)

vc 74
vc 74

Reputation: 38179

Assuming you mean you have a collection of Examples (e1, e2...):

var keys = examples.SelectMany(example => example.data.Keys)
                   .Distinct();

Upvotes: 5

Thomas Levesque
Thomas Levesque

Reputation: 292425

var keys = 
    (from ex in examples
     from key in ex.Data.Keys
     select key).Distinct();

Upvotes: 1

Related Questions