不能在字典上使用LINQ的.Count() [英] Can't use .Count() from LINQ on Dictionary
问题描述
当我在VB.NET中尝试以下操作时
When I try the following in VB.NET
Dim data = New Dictionary(Of String, Integer)
data.Count(Function(x) x.Value > 0) 'Compile-time error!
我收到 .Net小提琴:
公共重载ReadOnly属性计数为整数"的参数过多
Too many arguments to 'Public Overloads ReadOnly Property Count As Integer'
Visual Studio给我这个错误:
Visual Studio gives me this error:
公共只读属性计数为整数"没有参数,并且其返回类型无法编制索引.
'Public ReadOnly Property Count As Integer' has no parameters and its return type cannot be indexed.
但是以下方法确实有效:
The following does work though:
Enumerable.Where(data, Function(x) x.Value > 0).Count() 'Works!
data.Where(Function(x) x.Value > 0).Count() 'Works!
似乎找不到正确的过载.
It seems to not be finding the correct overload.
奇怪的是,足够的C#版本在Visual Studio中可以正常工作(但是在.NET Fiddle中失败-奇怪...发生了什么事?):
Oddly, enough the C# version of this works just fine in Visual Studio (but fails in .NET Fiddle - odd... what's going on?):
var data = new Dictionary<string, int>();
data.Count(x => x.Value > 0);
使用LINQ版本的 You need to use 这篇关于不能在字典上使用LINQ的.Count()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! .Count()
的正确方法是什么?推荐答案
AsEnumerable()
(see the Remarks section) to pick up the extension methods when the names clash.data.AsEnumerable().Count(Function(x) x.Value > 0)