复杂LINQ查询
考虑到这种结构复杂LINQ查询,linq,Linq,考虑到这种结构 List<IEnumerable<KeyValuePair<String, String>>> 更新 以上查询的结果(针对下面的列表运行)应为6 var list = new List<IEnumerable<KeyValuePair<String, String>>> { new [] { new KeyValuePair<String, String>("f
List<IEnumerable<KeyValuePair<String, String>>>
更新
以上查询的结果(针对下面的列表运行)应为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));
虽然我猜你要换新的。因为您想去掉数千个分隔符,所以您可能需要:Int32.Parseitem.Value、NumberStyles.AllowThusands、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);
你能提供一些原始列表内容的例子吗?你的原始查询没有什么意义,什么大于10,因为它看起来像是你在比较一个字符串?我不知道。他回来了,一切都变了。将我的代码更新为最新问题。
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);