См. Ниже, решение состояло в том, чтобы сгруппировать по имени, а затем выполнить итерацию по заменителям.
public class Letter
{
public int ID;
public string Name;
public string AdvCode;
public string Type;
public Boolean isDeleted;
public override string ToString()
{
return this.Name + "[" + this.ID + "]" + ":" + this.Type + " AdvCode=" + AdvCode + " Deleted: " + this.isDeleted.ToString();
}
}
public class Replacers
{
public int ID;
public string MainAdvCode;
public string ReplacesAdvCode;
}
class Program
{
static void Main(string[] args)
{
List<Letter> letters = GetLetters();
List<Replacers> replacables = GetReplacers();
foreach (IGrouping<string, Letter> g in letters.GroupBy(x => x.Name))
{
List<Letter> byName = g.ToList();
foreach (Replacers r in replacables)
{
if (byName.Any(x => x.AdvCode == r.MainAdvCode) && byName.Any(x=>x.AdvCode==r.ReplacesAdvCode))
{
//If we contain the code to replace...
foreach (Letter letter in byName.Where(x=>x.AdvCode==r.ReplacesAdvCode)){
letter.isDeleted = true;
}
}
}
}
Console.WriteLine("Result");
foreach (Letter l in letters.Where(x=>x.isDeleted))
{
Console.WriteLine(l.ToString());
}
Console.WriteLine("Press key to close");
Console.ReadKey();
}
public static List<Letter> GetLetters()
{
List<Letter> letters = new List<Letter>(){
new Letter(){
Name = "LettersA",
ID = 0,
AdvCode="455",
Type="pack",
isDeleted = false
},
new Letter(){
Name = "LettersA",
Type="addon",
ID = 1,
AdvCode="400",
isDeleted = false
},
new Letter(){
Name = "LettersB",
ID = 0,
AdvCode="955",
Type="big",
isDeleted = false
},
new Letter(){
Name = "LettersB",
Type="pack",
ID = 1,
AdvCode="455",
isDeleted = false
},
new Letter(){
Name = "LettersC",
ID = 0,
AdvCode="400",
Type="addon",
isDeleted = false
},
new Letter(){
Name = "LettersC",
Type="big",
ID = 1,
AdvCode="955",
isDeleted = false
},
new Letter(){
Name = "LettersC",
Type="pack",
ID = 2,
AdvCode="455",
isDeleted = false
},
new Letter(){
Name = "LettersD",
ID = 0,
AdvCode="455",
Type="pack",
isDeleted = false
},
new Letter(){
Name = "LettersD",
Type="none",
ID = 1,
AdvCode="019",
isDeleted = false
},
};
return letters;
}
public static List<Replacers> GetReplacers()
{
return new List<Replacers>(){
new Replacers(){
ID = 0,
MainAdvCode = "455",
ReplacesAdvCode = "400"
},
new Replacers(){
ID = 1,
MainAdvCode = "955",
ReplacesAdvCode = "400"
},
new Replacers(){
ID = 2,
MainAdvCode = "955",
ReplacesAdvCode = "455"
},
};
}