How to Get Sum Of Items using LINQ in C# ?

Below is a sample code snippet demonstrating how to use the Sum extension method to get the sum of experience of all employees using LINQ in C#.

How to Get Sum Of Items using LINQ in C# ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GinktageConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> employee = new List<Employee>();
            employee.Add(new Employee { Name = "Senthil Kumar", Experience = 6 });
            employee.Add(new Employee { Name = "Naveen", Experience = 4 });
            employee.Add(new Employee { Name = "Praveen", Experience = 1 });
            var query = (from i in employee
                         select i).Sum(a => a.Experience);
            Console.WriteLine(query);
            Console.ReadLine();
        }
    }
    public class Employee
    {
        public string Name { get; set; }
        public int Experience { get; set; }
    }
}


image

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

How to get the MAX row with group by in LINQ query using C# ?
Below is a sample code snippet demonstrating how to retreive the max row with the group by in LINQ Q...
How to Merge string Arrays and keep distinct values using LINQ in C# ?
Do you want to merge string arrays and yet want to keep the distinct values in C# ? . One of the opt...
Using GroupBy in LINQ and C#
Below is a sample code snippet demonstrating how to use GroupBy in LINQ and C#. Using GroupBy in LI...