循环中的循环

发布于 2024-10-04 12:40:41 字数 350 浏览 0 评论 0原文

嘿伙计们 - 我被这个问题困扰了,我只是想知道处理它的最佳方法是什么。

Foreach (var customer in CustomerList)
{
      Foreach (var appointment in AppointmentList)
      {
        if(appointment.customerId == customer.id)
           customer.appointments.add(appointment)


       }

}

这是我能想到的最简单的方法,但我不确定它是否是最有效的!

任何帮助都会很棒 -

谢谢。

Hey guys - I'm stuck with this problem and im just wondering what the best way to handle it is.

Foreach (var customer in CustomerList)
{
      Foreach (var appointment in AppointmentList)
      {
        if(appointment.customerId == customer.id)
           customer.appointments.add(appointment)


       }

}

this is the simplest way I can think to do it but im not sure if it's the most efficient!

Any help would be great-

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

街角迷惘 2024-10-11 12:40:41

可以将较短的列表预先分组;这应该会给你更好的性能 - 我找不到准确大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);
    }
}

You could perhaps pre-group the shorter list; this should give you better performance - I can't find citations to the exact big-O ratings since MSDN doesn't cite them, but it could be O(n + m) instead of O(n * m).

var apptsByCustomer = AppointmentList.ToLookup(appt => appt.customerId);

then you can use:

foreach (var customer in CustomerList) {
    foreach(var appointment in apptsByCustomer[customer.id]) {
        customer.appointments.add(appointment);
    }
}

Or without LINQ (from comments):

// 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);
    }
}
断舍离 2024-10-11 12:40:41

这又如何呢?

foreach (var customer in CustomerList)
{
    customer.AddRange( appointment.Where( a => a.CustomerId == customer.id));
}

对我来说,这似乎是一个清晰简洁的语法,它很好地解释了它的作用。

另外,我认为这里的性能应该很好,并且可能与您的原始代码大致相同。

What about this?

foreach (var customer in CustomerList)
{
    customer.AddRange( appointment.Where( a => a.CustomerId == customer.id));
}

For me this seems like a clear and concise syntax, and it explains what it does quite good.

Also, I think the performance should be fine here, and probably around the same as your original code.

海的爱人是光 2024-10-11 12:40:41

您可以使用 linq 从循环中删除嵌套,如下所示:

foreach (var customer in from customer in CustomerList
         from appointment in AppointmentList
         select customer)
{
    // ...
}

You could use linq to remove nesting from loops, like this:

foreach (var customer in from customer in CustomerList
         from appointment in AppointmentList
         select customer)
{
    // ...
}
紫竹語嫣☆ 2024-10-11 12:40:41

您可以将您的 customerList 设为 Dictionary,而不是由 组成。

然后,您不是循环列表,而是尝试获取值

Dictionary<int, Customer> customerList = new Dictionary<int, Customer>();
// populate your list
// ...
foreach (var appointment in AppointmentList)
{
    Customer customer;
    if (customerList.TryGetValue(appointment.customerID, out customer)){
        // found the customer
        customer.appointments.add(appointment)
}

这样,你就可以让 Dictionary 为你优化它。
请注意,如果您只执行此操作一次,则可能不值得进行太多优化除非您发现速度明显下降过早的优化并不值得付出努力

You could make your customerList a Dictionary instead consisting of <id, cust>.

Then, instead of looping the list you try to get the value

Dictionary<int, Customer> customerList = new Dictionary<int, Customer>();
// populate your list
// ...
foreach (var appointment in AppointmentList)
{
    Customer customer;
    if (customerList.TryGetValue(appointment.customerID, out customer)){
        // found the customer
        customer.appointments.add(appointment)
}

This way, you let Dictionary optimize it for you.
Do note that if you only perform this action once, it's probably not worth optimizing much unless you see a notable slowdown from it. Premature optimization is not really worth the effort

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文