:
foreach(Car car in listOfCars)
{
if (car.parts.Contains(partID))
{
return car;
}
}
Edit2: , , partIDs.
, ...
foreach(Car car in listOfCars)
{
foreach(Part part in car.parts)
{
if (part.id == partId)
{
return car;
}
}
}
Edit1: Depending on your use case, it may also make sense to maintain an βindexβ that maps to part identifiers for cars. Sort of:
var partIDToCar = Dictionary<string, Car>();
When you place parts in your cars, you update your index:
partIDToCar[partID] = car;
Then, this is a quick check to get the car:
if (partIDToCar.ContainsKey(partID))
{
return partIDToCar[partID];
}
source
share