I'd suggest creating a Dictionary out of the second thing you loop through. Then inside the foreach just use your Dictionary as a lookup for the value you want.
Updated with examples embedded foreach and Dictionary alternative:
List<ItemA> itemAs = new List<ItemA> {
new ItemA { id = 1, number = 10 },
new ItemA { id = 2, number = 15 },
new ItemA { id = 3, number = 20 }
};
List<ItemB> itemBs = new List<ItemB> {
new ItemB { id = 1, rate = 100 },
new ItemB { id = 2, rate = 150 },
new ItemB { id = 3, rate = 200}
};
List<ItemAB> itemABs = new List<ItemAB>();
// embedded foreach
foreach(ItemA itemA in itemAs)
{
foreach(ItemB itemB in itemBs)
{
if(itemA.id == itemB.id)
{
ItemAB itemAB = new ItemAB { id = itemA.id, number = itemA.number, rate = itemB.rate };
itemABs.Add(itemAB);
}
}
}
// using dictionary as lookup
Dictionary<int, ItemB> itemBLookup = itemBs.ToDictionary(x => x.id, y => y);
foreach (ItemA itemA in itemAs)
{
itemBLookup.TryGetValue(itemA.id, out ItemB itemB);
ItemAB itemAB = new ItemAB { id = itemA.id, number = itemA.number, rate = itemB.rate };
itemABs.Add(itemAB);
}