C# Linq Sum() Method


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements