Скажем, у меня есть два разных класса:
class Animal
{
string Name {get; set;}
int Age {get; set;}
string Description {get; set;}
}
class ButcheringMemo
{
string ButcherShopName {get; set;}
DateTime ButcheringTime {get; set;}
string AnimalName {get; set;}
}
Если бы у меня был список Animal и список ButcheringMemo, какой был бы лучший способ создания списка продуктов Animal, где Name изAnimal выглядит как AnimalName в ButcheringMemo?
Мой неаккуратный способ заключается в следующем:
List<Animal> animalsToButcher = new List<Animal>();
List<ButcheringMemo> butcheringMemos = getAllButcheringMemos();
List<string> animalNamesInButchering = new List<string>();
foreach (ButcheringMemo memo in butcheringMemos)
{
animalNamesInButchering.Add(memo.AnimalName);
}
List<Animal> animals = getAllAnimals();
foreach (Animal animal in animals)
{
bool isIn = false;
foreach (string name in animalNamesInButchering)
{
if (animal.Name == name)
isIn = true;
}
if (!isIn)
{
animalsToButcher.Add(animal);
}
}
return animalsToButcher;
Мне кажется, что должен быть лучший способ, чем иметь цикл for внутри цикла for.