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

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...