We are going to work with the following Employee class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp17 { public class Employee { public int ID { get; set; } public string Name { get; set; } public int Salary { get; set; } public string Department { get; set; } public static List<Employee> GetAllEmployees() { List<Employee> listStudents = new List<Employee>() { new Employee{ID= 101,Name = "Preety", Salary = 10000, Department = "IT"}, new Employee{ID= 102,Name = "Priyanka", Salary = 15000, Department = "Sales"}, new Employee{ID= 103,Name = "James", Salary = 50000, Department = "Sales"}, new Employee{ID= 104,Name = "Hina", Salary = 20000, Department = "IT"}, new Employee{ID= 105,Name = "Anurag", Salary = 30000, Department = "IT"}, new Employee{ID= 106,Name = "Sara", Salary = 25000, Department = "IT"}, new Employee{ID= 107,Name = "Pranaya", Salary = 35000, Department = "IT"}, new Employee{ID= 108,Name = "Manoj", Salary = 11000, Department = "Sales"}, new Employee{ID= 109,Name = "Sam", Salary = 45000, Department = "Sales"}, new Employee{ID= 110,Name = "Saurav", Salary = 25000, Department = "Sales"} }; return listStudents; } } class Program { static void Main(string[] args) { //Using Method Syntax var TotalSalaryMS = Employee.GetAllEmployees() .Sum(emp => emp.Salary); //Using Query Syntax var TotalSalaryQS = (from emp in Employee.GetAllEmployees() select emp).Sum(e => e.Salary); Console.WriteLine("Sum Of Salary = " + TotalSalaryMS); Console.ReadKey(); } } } |
Output: