【发布时间】:2011-08-18 14:06:18
【问题描述】:
有没有更好的方法来编写Where这个:
IDictionary<string, string> p = new Dictionary<string, string>();
p.Add("Apple", "1");
p.Add("Orange", "2");
p.Add("Pear", "3");
p.Add("Grape", "4");
p.Add("Pineapple", "5");
//This is a unique list
var retVal = p.Where(k => k.Key.Contains("Apple") || k.Key.Contains("Pear") || k.Key.Contains("Grape"));
下面的一些历史
我有一个字符串字典,如下所示:
IDictionary<string,string>
内容如下:
Apple,1
Orange,2
Pear,3
Grape,4
...many more
我怎样才能像这样只从我的字典中返回一些项目
if (true)
{
//return only 3 apple,pear&grape items out of the dozens in the list into a new variable
}
【问题讨论】:
-
...这不是过滤,而是“采取”3 个项目。还是这样?
-
您想退回哪三样东西?有很多方法可以只返回 3 个项目:基于索引顺序的前 3 个、基于字母顺序的前 3 个、随机 3 等。此外,“返回”是指作为方法调用的结果返回吗?
-
@xtofl 和 @squillman 请参阅上面的更新。谢谢。
-
@squillman - 我想把这三个项目绑定到我的下拉列表中
标签: c#