如何添加记录列表 [英] How do I add a list of records

查看:65
本文介绍了如何添加记录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要创建一个方法来在列表中添加条目列表(例如,我需要添加列表的所有收入)得到一个总额)



列表创建和打印如下,我需要在底部添加所有收入的总额:



Hi Guys,

I need to create a method to add a list of entries in a list (eg. I need to add all the incomes of a list to get a total)

List creation and print below, I need to add the total of all income at the bottom:

List<TaxRecord> taxrecords = new List<TaxRecord>()
        {
            new TaxRecord(53664,"John Smith","34 Johns St Sydney",true,6364664.99),
            new TaxRecord(53667,"Steve Henry","38 Victoria Rd Bulli",false,900.88),
            new TaxRecord(53656,"James Tee","65 True St Woonona",true,15678.00)
        };
        foreach (TaxRecord t in taxrecords)
        {
            t.Print();

        }





推荐答案

double total = taxrecords.Sum(item => item.Amount);



-KR


-KR


假设你的TaxRecord类有一个名为income的属性,类型为double,然后尝试下面的代码,它将总结列表中的所有收入。

Suppose that your TaxRecord class has a property named income which is of type double then try the below code which will sum up all the incomes within the list.
double total = 0;
foreach (TaxRecord t in taxrecords)
{
    total += t.income;
}


这篇关于如何添加记录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆