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

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

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -