C# Linq Sum() Method

CsharpProgrammingServer Side Programming

Find the sum of elements using the Linq Sum() method.

Here’s our list with integer elements.

List<int> list = new List<int> { 99, 34, 77, 75, 87, 35, 88};

Now find the sum using the Sum() method.

list.AsQueryable().Sum();

The following is an example to find the sum of elements of a list with integer elements.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      List<int> list = new List<int> { 99, 34, 77, 75, 87, 35, 88};
      int res = list.AsQueryable().Sum();
      Console.WriteLine("Sum = {0}", res);
   }
}

Output

Sum = 495
raja
Published on 14-Sep-2018 15:43:23
Advertisements