C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Next: IsEven returns true or false. Some numbers are grouped in the "false" category. The rest are grouped in the "true" category.
Odd, EvenC# program that uses group by operators
using System;
using System.Linq;
class Program
{
static void Main()
{
// Input array.
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Group elements by IsEven.
var result = from element in array
orderby element
group element by IsEven(element);
// Loop over groups.
foreach (var group in result)
{
// Display key and its values.
Console.WriteLine(group.Key);
foreach (var value in group)
{
Console.WriteLine(value);
}
}
}
static bool IsEven(int value)
{
return value % 2 == 0;
}
}
Output
False
1
3
5
7
9
True
2
4
6
8
Tip: More information on the foreach-loop, which evaluates IEnumerable, is available on this site.
Foreach