考虑到这种结构。。。
List<IEnumerable<KeyValuePair<String, String>>>
您将如何为以下伪代码编写LINQ查询。。。
SELECT
/* Count of how many [KeyValuePair] exists in [List] */
FROM
[List]
WHERE
[KeyValuePair].Key == "foo"
AND Int32.Parse([KeyValuePair].Value.Replace(".", "")) > 10
更新
根据下面的列表运行上述查询的结果应该是6(六(。。。
var list = new List<IEnumerable<KeyValuePair<String, String>>>
{
new []
{
new KeyValuePair<String, String>("foo", "1.1"),
new KeyValuePair<String, String>("foo", "1.2"),
new KeyValuePair<String, String>("foo", "1.3")
},
new []
{
new KeyValuePair<String, String>("foo", "0.1"),
new KeyValuePair<String, String>("foo", "0.2"),
new KeyValuePair<String, String>("foo", "0.3")
},
new []
{
new KeyValuePair<String, String>("foo", "2.1"),
new KeyValuePair<String, String>("foo", "2.2"),
new KeyValuePair<String, String>("foo", "2.3")
}
};
我从中得到6。。。
var result = list.Sum(item=>item.Count(kp=>kp.Key == "foo" && int.Parse(kp.Value.Replace(".",String.Empty))>10));
var result = list.SelectMany(ary =>aryx).Count(item => item.Key == "Foo" && Int32.Parse(item.Value.Replace(".", "")) > 10);
虽然我猜你正在替换".",因为你想去掉数千个分隔符,所以你可能想要:Int32.Parse(item.Value, NumberStyles.AllowThousands, CultureInfo.CurrentCulture)
确保你有适当的当前文化设置
为了清晰起见,我可能会这样写:
var result = list.SelectMany(ary => ary)
.Where(item => item.Key.Equals("Foo", StringComparison.CurrentCulture))
.Select(item =>
Int32.Parse(item.Value.Replace(".", ""))
.Count(value=> value> 10);
理解语法可能看起来像:
var q = from @array in list
from kvp in array
where kvp.Key.Equals("Foo", StringComparison.CurrentCulture)
select .Parse(item.Value.Replace(".", ""));
var result = q.Count(value => value > 10);