2014-04-02 67 views
1

我想创建一个array的值仅与我的dictionary的值等于零的元素。linq获取数组元素查询

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

请帮忙?

+0

是一个错字?你的意思是**值不等于零? –

回答

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

另一种方式:

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