Home > DeveloperSection > Forums > How to use GroupBy in linq query.
Ankit Singh

Total Post:341

Points:2389
Posted on    February-14-2016 11:13 PM

 LINQ LINQ 
Ratings:


 1 Reply(s)
 411  View(s)
Rate this:
I want  to use GroupBy in linq query.How to do this Please Help me.


Ankit Singh

Total Post:341

Points:2389
Posted on    February-14-2016 11:13 PM


using System;
using System.Linq;
namespace ConsoleDemo
{
public class Program
{
static void Main()
{

int[] array = { 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};
var result = array.GroupBy(a => IsEven(a));

foreach (var group in result)
{
Console.WriteLine("IsEven = {0}:", group.Key);

foreach (var value in group)
{
Console.Write("{0} ", value);
}
Console.WriteLine();
}
Console.Read();
}
static bool IsEven(int value)
{
return value % 2 == 0;
}
}
}

Don't want to miss updates? Please click the below button!

Follow MindStick