C# program to Find the sum of all numbers greater than 50 in an array Using LINQ.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { int[] intNumbers = new int[] { 10, 30, 50, 40, 60, 20, 70, 90, 80, 100 }; //Using Method Syntax int MSTotal = intNumbers.Where(num => num > 50).Sum(); //Using Query Syntax int QSTotal = (from num in intNumbers where num > 50 select num).Sum(); Console.WriteLine("Sum = " + QSTotal); Console.ReadKey(); } } } |
Output: 400