(C# Language) Practice Exercise on GroupJoin

Create a list of Category records with ID and Name as properties. Add 3 items to this list - (ID:1, Name:"For Men"), (2, "For Women") and (3, "For Kids"). Then create a list of SubCategory records with ID, Name and CategoryFK as properties. The property CategoryFK is a foreign key for the ID property of a Category. Add these items - (ID:1, CategoryFK:1, Name:"Shirts"), (2, 1, "T Shirts"), (3, 1, "Trousers"), (4, 2, "Skirts"), (5, 2, "Shoes"). Use the GroupJoin extension to present dresses of men under a heading "For Men", dresses of women under "For Women" and likewise for kids.
(Rev. 19-Mar-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Create a C# .NET Core Project

Create a C# console project for the latest version of .NET Core. Add a file called Models.cs and add record class for Category. This class has two properties called ID and Name.

Also add another record class for SubCategory. This class has three properties - ID, CategoryFK and Name.


// Models.cs file 

internal record class Category (Int32 ID, String Name);

internal record class SubCategory (Int32 ID, 
                                   Int32 CategoryFK, 
                                   String Name
                                  );

Video Explanation (see it happen!)

Please watch the following youtube video:

Program.cs File

Open the solution explorer so that we can complete the program.cs file.

First create a list of categories with the specified data. After that create a list of sub-categories with the specified data.

The question exercise requires that we have to basically group the SubCategory items under the CategoryFK so that records appear like this. Here 1, 2 and 3 are CategoryFK values


// if grouping is done on the CategoryFK  
// of SubCategories table then the data  
// should appear like this 

1
        Shirts
        T Shirts
        Trousers
2
        Skirts
        Shoes
3

But the problem is that we need to show the name of the CategoryFK so that the data looks like this:


// but the desired output should 
// show the name of the CategoryFK 

For Men
        Shirts
        T Shirts
        Trousers
For Women
        Skirts
        Shoes
For Kids

But the names of CategoryFK appear in the Categories table - which means that we need a Join as well.

Hence, the question exercise can be best solved by using the GroupJoin extension method.

Let's apply GroupJoin to the Categories list. This extension requires four arguments.

The first argument is the list to be joined. We have passed SubCategories as the argument here.

The second argument is a lambda that species the column of the first list. The column in our exercise question is Category.ID


// program.cs file 

List<Category> Categories = new()
                                  {
                                    new (1, "For Men"),
                                    new(2, "For Women"),
                                    new(3, "For Kids")
                                  };

List<SubCategory> SubCategories = new()
                                  {
                                    new(1, 1, "Shirts"),
                                    new(2, 1, "T Shirts"),
                                    new(3, 1, "Trousers"),
                                    new(4, 2, "Skirts"),
                                    new(5, 2, "Shoes")
                                  };

// see the linked video for explanation 
// of the four parameters 
var resultSet = Categories.GroupJoin(
                              SubCategories,
                              cat => cat.ID,
                              subcat => subcat.CategoryFK,
                              (category, list) => new {
                                For = category.Name,
                                Dresses = list
                              }
                            );

foreach (var item in resultSet)
{
  Console.WriteLine($"{item.For}");

  foreach (var dress in item.Dresses)
  {
    Console.WriteLine($"\t{dress.Name}");
  }
}

The third argument is a lambda that specifies the column of the second list. The column in our exercise question is SubCategory.CategoryFK

The fourth argument is a lambda delegate that receives two arguments. The GroupJoin method performs the necessary grouping and promises to provides us these two arguments. First, a Category instance that contains the group heading and the second argument is the list of SubCategory records grouped under that heading. This lambda has to return the final result. Our final result is an anonymous consisting of two properties - For and Dresses. The For property represents the group heading and the Dresses property contains the list of subcategories grouped under it.


// displayed output 

For Men
        Shirts
        T Shirts
        Trousers
For Women
        Skirts
        Shoes
For Kids

Lastly, we have set a foreach loop on the result set. First we display the name of the heading. Then a second nested loop displays the list of dresses grouped under that heading.

We can now run the project. We verify that the display is as expected. Thanks!


This Blog Post/Article "(C# Language) Practice Exercise on GroupJoin" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.