[英]How to remove all objects from List<object> where object.variable exists at least once in any other object.variable2?
[英]Remove all the objects from a list that have the same value as any other Object from an other List. XNA
我有兩個vector2列表:Position和Floor我正在嘗試這樣做:如果Position與Floor相同,則從List中刪除該位置。
這是我認為會起作用但它沒有:
public void GenerateFloor()
{
//I didn't past all, the code add vectors to the floor List, etc.
block.Floor.Add(new Vector2(block.Texture.Width, block.Texture.Height) + RoomLocation);
// And here is the way I thought to delete the positions:
block.Positions.RemoveAll(FloorSafe);
}
private bool FloorSafe(Vector2 x)
{
foreach (Vector2 j in block.Floor)
{
return x == j;
}
//or
for (int n = 0; n < block.Floor.Count; n++)
{
return x == block.Floor[n];
}
}
我知道這不是好方法,所以我怎么能做到呢? 我需要刪除與任何Floors Vector2相同的所有Positions Vector2。
================================================== =============================編輯:它的工作原理! 對於搜索如何操作的人來說,這是我對Hexxagonal答案的最終代碼:
public void FloorSafe()
{
//Gets all the Vectors that are not equal to the Positions List.
IEnumerable<Vector2> ReversedResult = block.Positions.Except(block.Floor);
//Gets all the Vectors that are not equal to the result..
//(the ones that are equal to the Positions).
IEnumerable<Vector2> Result = block.Positions.Except(ReversedResult);
foreach (Vector2 Positions in Result.ToList())
{
block.Positions.Remove(Positions); //Remove all the vectors from the List.
}
}
你可以做LINQ除外。 這將刪除不在Floor集合中的Positions集合中的所有內容。
result = block.Positions.Except(block.Floor)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.