HOW TO USE GROUPBY IN LINQ QUERY.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
LINQ 
 659  View(s)
Ratings:
Rate this:
I want  to use GroupBy in linq query.How to do this Please Help me.
  1. Ankit Singh

    Post:341

    Points:2389
    Re: How to use GroupBy in linq query.


    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;
    }
    }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!