如何使用C#计算字符串列表列表中的项目总和 [英] How do I calculate the sum of items in a List of List of string using C#

查看:79
本文介绍了如何使用C#计算字符串列表列表中的项目总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个List< List< string>>。这些List< string>中的每个项目都将用作显示报告的列。

我需要计算报告中每一列的总和(在将字符串转换为十进制之后),并将其显示为页脚。

不确定,项目数量在每个列表中将与其他列表匹配。



这意味着,应该转换列表中的每个项目并总结并添加到另一个列表< string>,其中将用于显示作为页脚行。



请建议我,在性能方面更好地使用哪种方法来实现这一点。



添加HTML编码(并修改标题以避免HTML标记) - OriginalGriff [/ edit]

Hi,
I have a List<List<string>>.Each item in these List<string>, will be used to display as columns of a report.
I need to calculate the sum (after converting the string to decimal),of each each column in the report and show it as the footer.
It is not sure, that the number of items in each list will match with other lists.

That means, each item of the lists should be converted and sum up and add in to another List<string>, which will be used to display as the footer row.

Please suggest me, which method would be better to use to achieve this in performance wise.

[edit]HTML Encodes added (and title modified to avoid HTML tags) - OriginalGriff[/edit]

推荐答案

制作回答谢尔盖在评论中的问题,为什么不使用List< decimal>首先?



这样可以写出诸如

To make an echo to Sergey's question in comments, why not using a List<decimal> in the first place?

This would allow to write things such as
decimal sum = listElements.Sum();


你好,



我不确定我理解你的问题但是如果你想要总结字符串列表中的所有项目并转换为总十进制值,你可以试试这个:



Hello,

I'm not sure I understand your question but If you want to sum all the items of the string list and convert to a total decimal value you can try this:

List<string> listElements = new List<string>();
decimal elementsTotal = listElements.Sum(x =>  Convert.ToDecimal(x));







我希望这可以帮到你



最好的问候,

谢谢!




I hope this can help you

Best Regards,
Thanks!


这篇关于如何使用C#计算字符串列表列表中的项目总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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