Web
Analytics
Group Join in LINQ with example | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Group Join:

A group join can produce a grouped hierarchical result where items in the second sequence are matched to items in the first sequence.

Unlike the previous inner join, the output of a group join can be organized hierarchically with PublicReviews grouped into their related item.

Note: to understand this concept please read my old blog Joins in LINQ with example

//GroupJoin Query

     var query = from items in totalitems join review in PublicReviewforItems on items.ItemNumber equals review.ReviewId into groupreviews select new { ItemsName = items.ItemName, Reviews = groupreviews };

Using into keyword we are getting hierarchical result.here groupreviews is a rangevariable it is showing sequence of reviews.

Nested Loop through query to get desired result

  foreach (var item in query)

            {

                Console.WriteLine(“Reviews for Item{0}”, item.ItemsName);

                foreach (var review in item.Reviews)

                {

                    Console.WriteLine(“-{0}”, review.Review);

                }

            }

            Console.ReadLine();

Complete Source Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JOINS
{
class Program
{
static void Main(string[] args)
{
Items[] totalitems ={
new Items{ ItemNumber=1, ItemName=”Computers”},
new Items{ ItemNumber=2, ItemName=”Electronics”},
new Items{ ItemNumber=3, ItemName=”TV”},
new Items{ ItemNumber=2, ItemName=”AC”},
};
PublicReview[] PublicReviewforItems = {
new PublicReview{ ReviewId=1, Review=”It is good”},
new PublicReview{ ReviewId=2, Review=”It is Electronic Items and working fine”},
new PublicReview{ ReviewId=3, Review=”It is best TV”},
new PublicReview{ ReviewId=4, Review=”It is Best for AC for Summers”},
new PublicReview{ ReviewId=5, Review=”It is excellent”},

new PublicReview{ ReviewId=6, Review=”It is best”}
};
//Inner Join Query
// var query = from items in totalitems join review in PublicReviewforItems on items.ItemNumber equals review.ReviewId select new { ItemsName = items.ItemName, Reviews = review.Review };

//Inner Join Loop
//foreach (var item in query)
//{
// Console.WriteLine(“{0}-‘{1}'”, item.ItemsName, item.Reviews);
//}
//Console.ReadLine();
//GroupJoin Query
var query = from items in totalitems join review in PublicReviewforItems on items.ItemNumber equals review.ReviewId into groupreviews select new { ItemsName = items.ItemName, Reviews = groupreviews };
//Group Join Loop
foreach (var item in query)
{
Console.WriteLine(“Reviews for Item{0}”, item.ItemsName);
foreach (var review in item.Reviews)
{
Console.WriteLine(“-{0}”, review.Review);
}
}
Console.ReadLine();
}
}
public class Items
{
public int ItemNumber;
public string ItemName;
}
public class PublicReview
{
public int ReviewId;
public string Review;
}
}

Output:

OutputGroupJoin

 

JOINS