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.

>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);

Leave a Reply