Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算特定郊区C的总余额#_C#_Arraylist - Fatal编程技术网

C# 计算特定郊区C的总余额#

C# 计算特定郊区C的总余额#,c#,arraylist,C#,Arraylist,有什么想法吗?感谢您在C#>=3.5中使用LINQ是最简单的方法: double total = 0; foreach (Customer Total in customers) total += Total.Balance; //Total Balance 抱歉,您没有描述您的客户道具,因此我假设它们。假设您的客户类中有余额属性 customers.Where(c => c.Suburb == "whatever").Sum(c

有什么想法吗?感谢您在C#>=3.5中使用LINQ是最简单的方法:

        double total = 0;

        foreach (Customer Total in customers)
            total += Total.Balance; //Total Balance

抱歉,您没有描述您的
客户
道具,因此我假设它们。

假设您的
客户
类中有
余额
属性

customers.Where(c => c.Suburb == "whatever").Sum(c => c.Balance);

使用以下Linq表达式

var bal = customers.Sum(c => c.Balance); // customers is the collection
您可以使用LINQ和每个组:

var balance=customers.GroupBy(g=>g.suburbname).Select(lg =>new { TotalBalance= lg.Sum(g => g.Balance)}); 
注意,您需要使用System.Linq添加


演示:

为什么你提到
id
点击
字段而不提出
Customer
结构?问题:
我如何计算总余额**按特定郊区**?
实际上我没有得到郊区,如果它也是字段,那么
何处
方法可以用来过滤感谢!!我如何将其称为我目前拥有的总余额?totBalance.Text=total.ToString();类似这样的东西?似乎如此=)如果它回答了你的问题,请接受。
var balance=customers.GroupBy(g=>g.suburbname).Select(lg =>new { TotalBalance= lg.Sum(g => g.Balance)}); 
var suburbGroups = customers
        .GroupBy(c => c.Suburb)
        .Select(g => new { Suburb = g.Key, Balance = g.Sum(c => c.Balance) })
        .OrderByDescending(x => x.Balance);

foreach(var grp in suburbGroups)
    Console.WriteLine("Suburb: {0}  Total-Balance: {1}", grp.Suburb, grp.Balance);