[Reserved] in C # using the Sum method of summing List collection operation

In the List operation of C #, sometimes we need to sum operation on a property List collection object, then you can use the Sum methods Lambda expressions to quickly implement this summation operation, using the Sum method can make the code simple easy to read, write and eliminating the need for tedious loop or foreach statement to traverse the List.

For example, in the collection of orders List orderList collection Order Amount Amount sum when the summary, the following statement can be used for rapid implementation.

var Amount = orderList.Sum (t => t.Amount);
In the above statement t represents the Order object entity orderList collection, other symbols may be used instead.

If List collection method corresponding to the type of digital system provides a simple, such as Int type, float type, Double type, Decimal type, for collection List amountList the like, it is also possible to use the Sum () This statement without particular requirements sum.

var Amount = amountList.Sum ();
the same effect as var Amount = amountList.Sum (t => t);

Note: The text reproduced from personal bloggers station IT technology small fun house , the original link is in C # using the Sum method of summing List collection operation _IT technology small fun house .

Guess you like

Origin blog.csdn.net/weixin_39650424/article/details/93380500