お客様の場合は、短いリストを事前にグループ化してください。これはあなたに良いパフォーマンスを与えるはずです - の正確な big-Oの評価は引用できませんが、はになる可能性があります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);
}
}
コンパイルできますか? IGroupingにインデクサがないと思った?あなたはToLookupを使いたいですか? ToLookupを使用する場合は、(ほとんど)O(n + m)にする必要があります。 – CodesInChaos
@CodeInChaos -Do!私は確かにToLookupを意味した –
それは私に良いprittyに見える - 悲しいことに、私たちはlinqを使用することはできません。 – Steoates