简体   繁体   English

如何总结C#集合中字段的值

[英]How can I sum up the value of a field in a C# collection

I have the following class: 我有以下课程:

public class ViewEvent {

    public long Elapsed { get; private set; }
    public string Description { get; private set; }

}

In my model I have a collection of instances of these classes: 在我的模型中,我收集了这些类的实例:

public IList<ViewEvent> Events { get; set; }

How can I get the total elapsedTime of all the ViewEvents in my Events collection? 如何获取事件集中所有ViewEvent的总经过时间?

With LINQ, Events.Sum(event => event.Elapsed); 使用LINQ, Events.Sum(event => event.Elapsed); .

As pointed out by Habib, be sure to add 正如哈比卜指出的那样,请务必添加

using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM