C# 计算特定郊区C的总余额#
有什么想法吗?感谢您在C#>=3.5中使用LINQ是最简单的方法: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
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);