Linq SUM on objects?

后端 未结 3 770
星月不相逢
星月不相逢 2021-01-17 21:59

I\'ve an object that represent a business data.

Basically, it\'s an object agregating some totals:

public class MyClass{
   public double MyDataOne {         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 22:25

    Write your own extension method:

    public static MyClass Sum(this IEnumerable source)
    {
        var result = new MyClass(); // all data will be zeros
    
        foreach(var item in source)
           result = result + item;
    
        return result;
    }
    

    Usage:

    var sum = myClasses.Sum();
    

提交回复
热议问题