Ad I'm developping a project in C# I recently came over an error I can't understand.
我正在用c#开发一个项目,最近我遇到了一个我无法理解的错误。
First of all here is the core
首先这里是核心
if (CanPlay(target.Coord.X, target.Coord.Y))
{
target.Owner = m.Player;
this.MoveList.Push(m);
// Groupes
var friends = GetAround(target).FindAll(i => i.Owner == target.Owner);
Groupe g = new Groupe { target };
foreach (Intersection fr in friends)
{
g.AddRange(GetGroupe(fr));
}
foreach (Intersection fr in friends)
{
this.Groupes.Remove(GetGroupe(fr));
}
this.Groupes.Add(g);
}
And here is the getGroupe function that causes the problem :
这是导致问题的getGroupe函数:
Groupe GetGroupe(Intersection i)
{
return this.Groupes.First(g => g.Contains(i));
}
So, at some random moments, when a move is done, I get the IEquatable Operation is not valid due to the current state of the object. Here is the stack trace :
因此,在某些随机时刻,当移动完成时,由于对象的当前状态,我得到IEquatable操作无效。这是堆栈跟踪:
InvalidOperationException: Operation is not valid due to the current state of the object
System.Linq.Enumerable.First[Groupe] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback)
System.Linq.Enumerable.First[Groupe] (IEnumerable`1 source, System.Func`2 predicate)
Assets.ObjetsDeJeu.Goban.GetGroupe (Assets.ObjetsDeJeu.Intersection i) (at Assets/ObjetsDeJeu/Goban.cs:155)
Assets.ObjetsDeJeu.Goban.PutRock (Assets.ObjetsDeJeu.Move m) (at Assets/ObjetsDeJeu/Goban.cs:142)
So the line 142 is
所以142号线是
g.AddRange(GetGroupe(fr));
and 155 is
和155是
return this.Groupes.First(g => g.Contains(i));
I don't understand since the this.Groupes is only empty at the very begenning. I did a test where I checked if it was empty before looking for a group but it didn't change anything ...
从这以后我就不明白了。群只在最开始是空的。我做了一个测试,在寻找一个组之前,我检查它是否是空的,但是它没有改变任何东西……
1 个解决方案
#1
3
The problem is that the call to First()
with the specified predicate does not yield any result. Use FirstOrDefault
instead and check for null
.
Change your code as follows to fix the problem:
问题是,使用指定谓词调用First()不会产生任何结果。使用FirstOrDefault并检查null。更改代码如下,以修复问题:
foreach (Intersection fr in friends)
{
var group = GetGroupe(fr);
if(group != null)
g.AddRange(group);
}
foreach (Intersection fr in friends)
{
var group = GetGroupe(fr);
if(group != null)
this.Groupes.Remove(group);
}
Groupe GetGroupe(Intersection i)
{
return this.Groupes.FirstOrDefault(g => g.Contains(i));
}
#1
3
The problem is that the call to First()
with the specified predicate does not yield any result. Use FirstOrDefault
instead and check for null
.
Change your code as follows to fix the problem:
问题是,使用指定谓词调用First()不会产生任何结果。使用FirstOrDefault并检查null。更改代码如下,以修复问题:
foreach (Intersection fr in friends)
{
var group = GetGroupe(fr);
if(group != null)
g.AddRange(group);
}
foreach (Intersection fr in friends)
{
var group = GetGroupe(fr);
if(group != null)
this.Groupes.Remove(group);
}
Groupe GetGroupe(Intersection i)
{
return this.Groupes.FirstOrDefault(g => g.Contains(i));
}