Thursday, 20 February 2014

group by clause in linq query

The group clause returns a sequence of grouping of the values. Creating the list of object and list array.
List<Employee> players = new List<Employee>
       {
           new Employee { Name = "John", Designation= "Manager" },
           new Employee { Name = "Ross", Designation = "Worker" },
           new Employee { Name = "Eric", Designation = "leader" },
           new Employee { Name = "Josh", Designation = "Manager" },
           new Employee { Name = "Mandy", Designation = "Worker" },
           new Employee { Name = "Flora", Designation = "Manager" },
           new Employee { Name = "Garry", Designation = "Worker" },
           new Employee { Name = "Joseph", Designation = "Worker"},
           new Employee { Name = "Murray", Designation = "leader"},
           new Employee { Name = "Henry", Designation = "leader"},
           new Employee { Name = "Watson", Designation = "Worker"},
           new Employee { Name = "Linda", Designation = "Manager"}
       };
Before that create the Employee class to retain all the values;
  public class Employee
    {
        public string Name { get; set; }
        public string Designation { get; set; }
    }
Calling the group by class in the applications to group all the particular values;
  var groups = from p in players
                         group p by p.Designation into g
                         select new { GroupName = g.Key, Members = g };
When you use into, you must continue with the query, and eventually end it with either a select statement or another group clause
Getting the full code as below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace apptest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> emp = new List<Employee>
       {
           new Employee { Name = "John", Designation= "Manager" },
           new Employee { Name = "Bob", Designation = "Worker" },
           new Employee { Name = "Eric", Designation = "leader" },
           new Employee { Name = "Josh", Designation = "Manager" },
           new Employee { Name = "Somesh", Designation = "Worker" },
           new Employee { Name = "Flora", Designation = "Manager" },
           new Employee { Name = "Giitu", Designation = "Worker" },
           new Employee { Name = "Joseph", Designation = "Worker"},
           new Employee { Name = "Soni", Designation = "leader"},
           new Employee { Name = "Henry", Designation = "leader"},
           new Employee { Name = "Watson", Designation = "Worker"},
           new Employee { Name = "Linda", Designation = "Manager"}
       };
            var groups = from e in emp
                         group e by e.Designation into g
                         select new { GroupName = g.Key, Members = g };
            foreach (var g in groups)
            {
                Console.WriteLine("Members of {0}", g.GroupName);
                foreach (var member in g.Members)
                {
                    Console.WriteLine("---{0}", member.Name);
                }
            }
        }
    }
    public class Employee
    {
        public string Name { get; set; }
        public string Designation { get; set; }
    }
}
 Showing the output valus as;

No comments:

Post a Comment

Others

You will like to read it