【问题标题】:count occurrences in for loop计算 for 循环中的出现次数
【发布时间】:2017-06-17 12:50:29
【问题描述】:

例如。

如果 James、Joe 和 John 访问了一家商店,我如何计算循环中访问该商店的人数而不是显示 3 次数据。真的,我需要计算那 1 家商店中有多少 B.customer_name。因此,对于每个 customer_name 计算出现次数。

    For Each A As customer_service.customer_details In C.customers

        Dim B As customer_service.customer = customer_dl.customer_details(A.customer_id)
        display_customer.Text &= "<p>" & A.customer_shops & " " & B.customer_name & "</p>"

    Next

【问题讨论】:

    标签: arrays vb.net loops for-loop


    【解决方案1】:

    要自己获取每个客户,然后获取计数,您通常使用GroupBy 来获取每个客户的数据,然后使用Count 来获取计数。

    在 C# 中,类似于:

    foreach (var customerVisits in C.customers.GroupBy(c => c.Name))
    {
       int visitCount = customerVisits.Count()
    }
    

    【讨论】:

    • 在 VB 中类似于:对于每个 customerVisits In C.customers.GroupBy(Function(c) c.Name) : Dim visitCount = customerVisits.Count() : Next (visitCount 的声明可以包含类型,具体取决于您是否希望它推断类型或如果您认为自己知道并被证明是错误的 squaw)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2021-09-09
    • 2023-03-21
    相关资源
    最近更新 更多