In this article, I will demonstrate how we can use GroupBy and ToLookup operators of Language-Integrated Query (LINQ). GroupBy and ToLookup operators are the part of grouping data in LINQ. Grouping refers to the operations of putting data into groups so that the elements in each group share a common attribute. GroupBy & ToLookup return a collection that has a key and an inner collection based on a key field value. The execution of GroupBy is deferred whereas that of ToLookup is immediate. A LINQ query syntax can end with the GroupBy or Select clause. A LINQ query syntax can end with the GroupBy or Select clause.
- CREATE TABLE [dbo].[Employee](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Position] [nvarchar](50) NULL,
- [Office] [nvarchar](50) NULL,
- [Salary] [money] NULL,
- [Department] [nvarchar](50) NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
Step 2
Open Visual Studio 2015 and create a new console application with a meaningful name.
Step 3
Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item.
Screenshot for adding Entity Framework 1
After clicking on the new item, you will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.
Screenshot for adding Entity Framework 2
After you click on "Add a window", the wizard will open. Choose EF Designer from database and click next.
Screenshot for adding entity framework 3
After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter dot (.). Choose your database and click on OK.
Screenshot for adding entity framework 4
Connection will be added. If you wish save connect as you want. You can change the name of your connection below. It will save connection in web config then click on Next.
Screenshot for adding entity framework 5
After clicking on NEXT another window will appear choose database table name as shown in the below screenshot then click on Finish.
Screenshot for adding entity framework 6
Entity framework will be added and respective class gets generated under Models folder.
Screenshot for adding entity framework 7
Following class will be added,
- namespace GroupingData_Demo
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Employee
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Position { get; set; }
- public string Office { get; set; }
- public Nullable<decimal> Salary { get; set; }
- public string Department { get; set; }
- }
- }
Step 4
Write a program to call database class
Example 1 - GroupBy
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace GroupingData_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (DBModel db=new DBModel())
- {
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- var result = employeeList.GroupBy(e => e.Department);
-
- foreach(var group in result)
- {
- Console.WriteLine("Department Name:{0}\nNumber of Employee:{1}",group.Key,group.Count());
- Console.WriteLine();
- }
-
- Console.Read();
- }
- }
- }
- }
Output
Example 2 - GroupBy
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace GroupingData_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (DBModel db=new DBModel())
- {
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- var result = employeeList.GroupBy(e => e.Department);
-
- foreach(var group in result)
- {
- Console.WriteLine();
- Console.WriteLine("Department Name-->" + group.Key);
- Console.WriteLine();
-
- foreach (Employee emp in group)
- {
- Console.WriteLine("Employee Name:\t" + emp.Name);
-
- }
- }
-
- Console.Read();
- }
- }
- }
- }
Example 3 - GroupBy
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace GroupingData_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- using (DBModel db=new DBModel())
- {
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- var result = employeeList.GroupBy(e => e.Department);
-
- foreach(var group in result)
- {
- Console.WriteLine("Department Name:{0}\nTotal Salary of Deaprtment:{1}",group.Key,group.Sum(x=>x.Salary));
- Console.WriteLine();
- }
-
- Console.Read();
- }
- }
- }
- }
Output
Example 1 - ToLookup
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace GroupingData_Demo
- {
- class ToLookup
- {
- static void Main()
- {
- using (DBModel db = new DBModel())
- {
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- var result = employeeList.ToLookup(e => e.Department);
-
- foreach (var group in result)
- {
- Console.WriteLine("Department Name-->" + group.Key);
- Console.WriteLine();
- }
- }
- }
- }
- }
Output
Example 2 - ToLookup
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace GroupingData_Demo
- {
- class ToLookup
- {
- static void Main()
- {
- using (DBModel db = new DBModel())
- {
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- var result = employeeList.ToLookup(e => e.Department);
-
- foreach (var group in result)
- {
- Console.WriteLine();
- Console.WriteLine("Department Name-->" + group.Key);
- Console.WriteLine();
-
- foreach (Employee emp in group)
- {
- Console.WriteLine("Employee Name:\t" + emp.Name);
-
- }
- }
-
- Console.ReadLine();
- }
- }
- }
- }
Output