简体   繁体   English

如何检查字典/集合是否可以迭代?

[英]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.

相关问题 如何创建可以迭代的类的集合? - How to create a collection of classes that can be iterated over? foreach如何知道迭代集合被修改了? - How can foreach know the iterated collection was modified? C#Parallel - 将项目添加到正在迭代的集合中,还是等效的? - C# Parallel - Adding items to the collection being iterated over, or equivalent? 如何在传递给视图以作为有序列表进行迭代的 ViewModel 集合中获取 jQuery 可选对象的值? - How can I get the value of a jQuery selectable object in a ViewModel collection I passed to my view to be iterated through as an ordered list? 为什么未迭代的Dictionary集合被视为只读? - Why is a Dictionary collection that is not being iterated thru being treated as read-only? 如何制作可以迭代的ApplicationUser类型的列表? - How to make a list of ApplicationUser type that could be iterated over? 如何限制在foreach循环中迭代的元素数量? - How do I limit the number of elements iterated over in a foreach loop? 如何在foreach循环中对迭代的元素进行分段 - How do I segment the elements iterated over in a foreach loop 为什么我不能用 linq 迭代可以用 foreach 迭代的东西? - Why can I not iterate over something with linq that can be iterated over with foreach? 在字典的值集合上应用函数 - Applying a function over a Dictionary's value collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM