[英]How to check that a dictionary / collection can be iterated over?
I have some code that works on a dictionary and Using simple linq functions to find some key values within the dictionary however recently I am getting the following error ... 我有一些适用于字典的代码,并且使用简单的linq函数在字典中查找了一些键值,但是最近我遇到了以下错误...
InvalidOperationException: Operation is not valid due to the current state of the object
System.Linq.Enumerable.Iterate[KeyValuePair`2,Int32] (IEnumerable`1 source, Int32 initValue, System.Func`3 selector)
How do I confirm that the operation is safe to be carried out and thus avoid this error? 如何确认操作安全,从而避免此错误?
Code sample causing the problem ... 导致问题的代码示例...
public void UpdateBounds()
{
lock (Voxels)
{
Start = new IntVector3(Voxels.Min(v => v.Key.X), Voxels.Min(v => v.Key.Y), Voxels.Min(v => v.Key.Z));
End = new IntVector3(Voxels.Max(v => v.Key.X), Voxels.Max(v => v.Key.Y), Voxels.Max(v => v.Key.Z));
}
}
Looks like you're doing an operation on an empty Dictionary object. 看起来您正在对空的Dictionary对象进行操作。
Check if it contains anything before executing that statement. 执行该语句之前,请检查其中是否包含任何内容。
Dictionary.Count
is your guy. Dictionary.Count
是你的家伙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.