Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Sum values in a List with multiple columns

Was following tutorial here https://www.tutorialspoint.com/chash-linq-sum-method to Sum up values in a list. But I have multiple columns so code fails at:

            double res = ProductList.AsQueryable().Sum();
            Console.WriteLine(res);

see end of the file here https://dotnetfiddle.net/vmtTuP for issue.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t need AsQueryable() and all you need to do is specify the field of your object you wish to sum – for example if you wanted to sum Tax:

double res = ProductList.Sum(x => x.Tax);
Console.WriteLine(res);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading