c# - Linq SUM on objects? -


i've object represent business data.

basically, it's object agregating totals:

public class myclass{    public double mydataone {get;set;}    public double mydatatwo {get;set;}    public double mydatathree {get;set;}     public static myclass operator +(myclass data1, myclass data2){       return new myclass{mydataone = data1.mydataone + data2.mydataone, mydatatwo=data1.mydatatwo+data2.mydatatwo, mydatathree=data1.mydatathree+data2.mydatathree };    } } 

now, if i've ienumerable<myclass> myclasses, there somethings can implement in myclass make this:?

myclasses.sum(d=>d); 

because me, way object additioned must knowledge of object , not caller(if 1 day i've 1 data more, don't want in whole code see used).

thank you

write own extension method:

public static myclass sum(this ienumerable<myclass> source) {     var result = new myclass(); // data zeros      foreach(var item in source)        result = result + item;      return result; } 

usage:

var sum = myclasses.sum(); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -