c# - How do you get the sum of the product of two properties in an IEnumerable using LINQ? -


i have ienumerable<dynamic> sql query using dapper, , add product of 2 properties of dynamic objects in ienumerable.

i tried:

decimal total = orderdetails.aggregate((workingtotal, detail) =>      workingtotal + (detail.quantity * detail.unitprice)); 

but returns object cannot converted decimal.

i use sum instead of aggregate:

decimal total = orderdetails.sum(x => (decimal) (x.quantity * x.unitprice)); 

depending on situation is, can imagine potentially working without casts, or needing more casts... it's not easy tell dynamic.


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

php - Improving script execution time -

How to provide Authorization & Authentication using Asp.net, C#? -