Hi
How to Sum column value with where condition using Linq
Thanks
Hi
How to Sum column value with where condition using Linq
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Dec 1, 2022, 10:25 AM EST
Hi Ramco,
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 GetAllEmployees() { List listStudents = new List() { 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() .Where(emp => emp.Department == "IT") .Sum(emp => emp.Salary); //Using Query Syntax var TotalSalaryQS = (from emp in Employee.GetAllEmployees() where emp.Department == "IT" select emp).Sum(e => e.Salary); Console.WriteLine("IT Department Total Salary = " + TotalSalaryQS); Console.ReadKey(); } }// Output
IT Department Total Salary = 120000