操作无效,由于对象的当前状态 - 的LINQ上列出 [英] Operation is not valid due to the current state of the object - Linq on List

查看:387
本文介绍了操作无效,由于对象的当前状态 - 的LINQ上列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行LINQ查询超过列表时,被抛出此错误。

This error is being thrown when running a Linq query over a List.

我使用Unity3D 3.0 C#(Unity3D使用单声道2.6)。 Unity3D,据我所知,是单线程的。它的工作原理通过附加继承里继承,在游戏对象,脚本(C#的.cs文件)。此外,统一控制脚本,所以你不能使用构造函数实例化和系列化。

I am using Unity3D 3.0 with C# (Unity3D uses Mono 2.6). Unity3D, as far as I know, is single-threaded. It works by attaching "scripts" (c# .cs files) that inherit a baseclass, to a "GameObject". Also, Unity controls instantiation and serialization of scripts so you can't use constructors.

我有一个RoadNetwork脚本保存到RoadNodes和RoadCurves参考,这两个定位通过RoadNetwork独立的,并注册/注销自己。与RoadNetwork我已经把小工厂,在RoadNode和RoadCurve那些挂钩本身到一个游戏物体的辛勤工作。

I have a RoadNetwork script that holds a reference to RoadNodes and RoadCurves, both of which locate RoadNetwork via a singleton and register/deregister themselves. I've put "mini-factories" in RoadNode and RoadCurve that do the hard work of hooking themselves up to a gameobject.

RoadNode首先检查,以确保有心不是已经处于同一个位置,这样做的一个节点:

RoadNode first checks with RoadNetwork to make sure there isnt already a node at that same position, by doing this:

public static RoadNode2 New(float x, float y, float z)
{
    //First try to find an existing one
    var rn = RoadNetwork.Instance.GetNodeAtPosition(new Vector3(x, y, z))
             ?? UnityReferenceHelper.GetNewGameObjectFor<RoadNode2>(
                 "RoadNode_" + (RoadNetwork.Instance.Nodes.Count + 1).ToString("D3"),
                 RoadNetwork.Instance.transform.FindChild("Nodes"));

    rn.Position = new Vector3(x, y, z);

    rn.gameObject.active = true;

    return rn;
}



凡RoadNetwork适当的方法是:

Where the appropriate method in RoadNetwork is:

public RoadNode2 GetNodeAtPosition(Vector3 position)
{
    var tempList = new List<RoadNode2>();

    return tempList.Single(x => x.Position == position);
}



tempList在缩小问题的一个尝试,但我得到完全相同的错误。它应该是Nodes.Single(...,但我怀疑它很重要。我得到了同样的错误,如果我直接在new()方法调用LINQ查询。

tempList was an attempt at narrowing down the problem but I get precisely the same error. It should be "Nodes.Single(...", but I doubt it matters. I get the same error if I call the Linq query directly in the New() method.

所以,是的,这个异常抛出,并指出我到tempList.Single()行。会的原因是什么呢?

So yes, this Exception throws and points me to that tempList.Single() line. What would the reason be?

推荐答案

someEnumerable.Single(...)如果在 someEnumerable 只有一个元素不是抛出一个异常,鉴于您刚才宣布 tempList 是一个空表,它总是会抛出异常。

someEnumerable.Single(...) throws an exception if there is not exactly one element in someEnumerable. Given that you just declared tempList to be an empty list, it will always throw an exception.

如果你想检索如果没有的元素,使用的SingleOrDefault 。(这仍然会抛出一个异常,如果枚举包含的更多的多个元素。)如果你想要的第一个的元素,让你的枚举允许包含任意数量的元素,使用首先 FirstOrDefault (返回的情况下)$ C>(如果枚举不包含任何元素抛出异常)。

If you want to retrieve null if there are no elements, use SingleOrDefault. (This will still throw an exception if the enumerable contains more than one element.) If you want the first element, so that your enumerable is allowed to contain any number of elements, use First (throws an exception if the enumerable contains no elements) or FirstOrDefault (returns null in the case).

最后,如果你想简单地检查是否有匹配给定的谓词列表中的任何元素,使用任何

Finally, if you want to simply check if there are any elements of a list matching a given predicate, use Any.

这篇关于操作无效,由于对象的当前状态 - 的LINQ上列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆