簡體   English   中英

“序列不包含匹配元素”C#

[英]"Sequence contains no matching element" C#

我正在制作一個應用程序,其中Container具有XYZ屬性的Container 這些集裝箱裝在Ship 所有容器都是正常的、 CooledValuable value 的要求是 X -1 和 X + 1 為空。 所以基本上貴重Container旁邊的容器一定是空的。 當它們為空時,我希望它返回中間的Container

我用XYZ預先制作了一個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()返回一個空容器,其中另一個ContainerX - 1X + 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM