Вы могли бы возможно предварительно сгруппировать более короткий список; это должно дать вам лучшую производительность - я не могу найти цитаты с точными рейтингами big-O, поскольку MSDN их не цитирует, но может быть O (n + m) вместо O (n * m).
var apptsByCustomer = AppointmentList.ToLookup(appt => appt.customerId);
тогда вы можете использовать:
foreach (var customer in CustomerList) {
foreach(var appointment in apptsByCustomer[customer.id]) {
customer.appointments.add(appointment);
}
}
Или без LINQ (из комментариев):
// this bit is **broadly** comparable to ToLookup...
Dictionary<int, List<Appointment>> apptsByCustomer =
new Dictionary<int, List<Appointment>>();
List<Appointment> byCust;
foreach(Appointment appt in AppointmentList) {
if (!apptsByCustomer.TryGetValue(appt.customerId, out byCust)) {
byCust = new List<Appointment>();
apptsByCustomer.Add(appt.customerId, byCust);
}
byCust.Add(appt);
}
foreach (Customer cust in CustomerList) {
if (apptsByCustomer.TryGetValue(cust.id, out byCust)) {
foreach (Appointment appt in byCust) cust.appointments.Add(appt);
}
}