santBart
santBart

Reputation: 2496

linq get array elements query

I would like to create an array of values only with elements of my dictionary which values is equal zero.

Dictionary<string, int> dict = new Dictionary<string, int>();
int notZeroValues = dict.Values.ToArray();  //sth here to get these elements efficiently

Please help?

Upvotes: 1

Views: 76

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

dict.Where(x => x.Value != 0).Select(x => x.Value).ToArray();

Another way:

dict.Values.OfType<int>().Where(x => x != 0).ToArray();

Upvotes: 2

Related Questions