[英]"Sequence contains no matching element" C#
我正在制作一个应用程序,其中Container
具有X
、 Y
和Z
属性的Container
。 这些集装箱装在Ship
。 所有容器都是正常的、 Cooled
或Valuable
。 value 的要求是 X -1 和 X + 1 为空。 所以基本上贵重Container旁边的容器一定是空的。 当它们为空时,我希望它返回中间的Container
。
我用X
, Y
和Z
预先制作了一个List<Container>
。
public void ConstructShip()
{
for (int x = 1; x < widthX + 1; x++)
{
for (int y = 1; y < lengthY + 1; y++)
{
for (int z = 1; z < heightZ + 1; z++)
{
SortedContainers.Add(new Container(x, y, z, true, false, false, 0)); //xyz, (bool)empty, (bool)valuable, (bool)cooled, weight
}
}
}
}
问题:我试图让方法CheckValuableAndReturnPlace()
返回一个空容器,其中另一个Container
的X - 1
和X + 1
的坐标是Empty
,也不Valuable
。
private Container CheckValuableAndReturnPlace()
{
Container freeContainer = SortedContainers
.First(c => c.X > 0 && c.X <= WidthX && c.Empty && c.Z <= HeightZ &&
SortedContainers.First(c2 => c2.X == c.X - 1).Empty &&
SortedContainers.First(c2 => c2.X == c.X + 1).Empty &&
!SortedContainers.First(c2 => c2.X == c.X - 1).Valuable &&
!SortedContainers.First(c2 => c2.X == c.X + 1).Valuable)
.FirstOrDefault();
if (freeContainer != null)
{
return freeContainer;
}
return new Container();
}
澄清一下:我想要一个Empty
Container
(列表中的所有Container
都已经存在),其中的侧面,因此 X -1 和 X +1 也是Empty
。 或者,如果位置是 X = 1,那么只有 X = 2 需要为空且无价值或 X = MaxXLength 比仅 X = MaxXlength - 1 需要为空且无价值
单元测试:
[TestInitialize()]
public void Initialize()
{
Ship ship = new Ship(4, 3, 3); //lengthX, widthY, heightZ
for (int x = 1; x < 5; x++)
{
for (int y = 1; y < 4; y++)
{
for (int z = 1; z < 4; z++)
{
ship.SortedContainers.Add(new Container(x, y, z, true, false, false, 0));
}
}
}
}
[TestMethod()]
public void CheckValuableAndReturnPlaceTest_Working()
{
Container freeContainer = ship.CheckValuableAndReturnPlace();
Container expectedContainer = new Container(1, 1, 1, true, false, false, 0);
Assert.AreEqual(JsonConvert.SerializeObject(expectedContainer), JsonConvert.SerializeObject(freeContainer));
}
最后编辑:
抱歉,我的问题太含糊了,我尽力不提供不需要的多余信息,但我想通了。 感谢一个人试图给我一个答案:p。
我假设这个问题比标题更多。 但是,如果标题中提到的错误阻止了您,请参阅此堆栈帖子。 序列不包含匹配元素
如果我理解正确,你想找到第一个容器,它和它的邻居都是空的,没有价值。 我不太明白为什么,但这似乎是你的帖子所描述的。
您提到想要找到所有侧面也满足这些要求但仅参考 x 方向的容器。
我想要一个空容器(列表中的所有容器都已经是),其中的边,所以 X -1 和 X +1 也是空的。
到目前为止,我正在关注单词(“sides”)而不是逻辑(仅提到 x),因为您正在寻求有关逻辑的建议。
如果我是你,我会依靠一些额外的封装来解决这个问题。 单独对盒子进行排序并跟踪它们的位置。 你可以这样做的一种方法看起来像这样。
public class ContainerGrid{
public List<List<List<Container>>> Locations = new List<List<List<Container>>>();
public List<Container> OrderedList;//you can keep sorting the containers and their current locations separate but still cleanly managed this way.
public bool IsEmpty(int x, int y, int z){
//interpreting what you said about needing to be empty and not valuable to be viable.
return containers[x][y][z].empty && !containers[x][y][z].valuable;
}
public bool IsEmptyAndNeighborsEmpty(int x, int y, int z){
//i am unsure if you intended sides to be only in the x direction
//or if that was just a part of how you explained it in the question
return IsEmpty(x,y,z)
&& IsEmpty(x-1,y,z) && IsEmpty(x+1,y,z);
//&& IsEmpty(x,y-1,z) && IsEmpty(x,y+1,z);
//&& IsEmpty(x,y,z-1) && IsEmpty(x,y,z+1);
}
public bool HasLocation(int x, int y, int z){
return Locations[x]!=null && Locations[x][y] != null && Locations[x][y][z]!=null;
}
public bool Add(Container container){
if(!HasLocation(container.x,container.y,container.z)){
if(Locations[container.x]==null)
AddAt<List<List<Container>>>(Locations, container.x, new List<List<Container>>(), false);
if(Locations[container.x][container.y]==null)
AddAt<List<Container>>(Locations[container.x], container.y, new List<Container>(), false);
if(Locations[container.x][container.y][container.z]==null)
AddAt<Container>(Locations[container.x][container.y], container.z, container, true);
OrderedList.Add(container);
}else return false;
}
public bool AddAt<T>(List<T> list, int index, T value, bool nullUntil){
while(list.Count<=index+1) list.add(nullUntil ? null : value);
list[index]=value;
}
}
封装高维数组的方法有很多。 我无法从你的问题的上下文中辨别出什么是最好的。
如果您打算像船坞那样建模,其中盒子(理论上)在网格模式中,并且您想以这种方式检查邻居,那么三维列表可能是可行的方法。
如果你想要一个平面网格,每个垂直的容器堆栈作为它自己的列表,你也可以这样做,它是任意的,只取决于你想用它做什么。
但就目前而言,您可以简单地检查那里的空虚和缺乏
public ContainerGrid ShipYard;
public Container FindFirstEmptyContainer(){
return ShipYard.OrderedList.FirstOrDefault(x=>ShipYard.IsEmptyAndNeighborsEmpty(x.x,x.y,x.z));
}
随意排序列表,但仍保持相同的空间关系。
这是否回答了您要问的问题? 您能否澄清此答案未涵盖的任何内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.