Understanding group by in LINQ by example


We will describe it by a full example “the full code in the end of the post for copy”

In this example we will create 2 classes , the “Task” class which has the task properties and the “TasksGroupedItem” which has 3 properties:

Task day :  it will contain the task day that we will group by

List<Task>  : it will contain list of tasks per Task Day

Then we will create a function that takes List<Task> and return List<TasksGroupedItem> , here is the code


1-The Task Class

Task Class

Task Class

2-The TasksGroupedItem  class

Tasks Grouped Item Class

Tasks Grouped Item Class

3-The GetUserTasksGroupedList method  that will  return  List<TasksGroupedItem> using LINQ “See code comments above each line in the image below-click for full size view”

Function to return list tasks of group by task day using LINQ

Function to return list tasks of group by task day using LINQ

The full code for copy

/// <summary>
/// User tasks grouped
/// </summary>
public class TasksGroupedItem
public DateTime TaskDay { get; set; }

public List<Task> UserTasksList { get; set; }
/// <summary>
/// Task
/// </summary>
public class Task
public string CreatedBy { get; set; }
public string Manager { get; set; }
public string TaskStatus { get; set; }
public string StartDate { get; set; }
public string FinishDate { get; set; }
public DateTime TaskCreatedDay{ get; set; }

/// <summary>
/// Get User Tasks Grouped List
/// </summary>
/// <param name=”UserTasksList”>User Tasks List to be grouped by task created date</param>
/// <returns></returns>
public List<TasksGroupedItem>  GetUserTasksGroupedList(List<Task> UserTasksList)
List<TasksGroupedItem> userTasksGroupedList = new List<TasksGroupedItem>();

var requestTaskItemGroupedList = UserTasksList.GroupBy(userTask => userTask.TaskCreatedDay);

TasksGroupedItem userTasksGroupedItem = null;

/* IGrouping<DateTime, UserTask> is a generic dictionary which contains the key as a DateTime and
the value as UserTask Object */
foreach (IGrouping<DateTime, Task> userTaskGroupDictionary in requestTaskItemGroupedList)
userTasksGroupedItem = new TasksGroupedItem();

// the key in this dictionary will be the field which we grouped by in this case is CreateDate column
userTasksGroupedItem.TaskDay = userTaskGroupDictionary.Key;

//Convert the dictionary to C# generic List<Tasks> per the task day (task day in above line)
userTasksGroupedItem.UserTasksList = userTaskGroupDictionary.ToList<Task>();

//Add the TasksGroupedItem to the list of TasksGroupedItem “List<TasksGroupedItem>” that will return

return userTasksGroupedList;

Hope you understand that and hope it will help


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s