Group join in lambda C#
August 23, 2022 2022-08-23 20:22Group join in lambda C#
- Create a console application and create a customer class and a country class
public class Customer
{
public int CustomerCode { get; set; }
public string CustomerName { get; set; }
public int TotalOrder { get; set; }
}
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
}
2. Create a list of customers and countries.
var customerList = new List()
{
new Customer{CustomerCode=101,CustomerName = "Steve", TotalOrder = 2, CountryCode="US"},
new Customer{CustomerCode=102,CustomerName = "Mark", TotalOrder = 4, CountryCode="IN"},
new Customer{CustomerCode=103,CustomerName = "Bill", TotalOrder = 7, CountryCode="US"},
new Customer{CustomerCode=104,CustomerName = "Dell",TotalOrder = 6, CountryCode="US"}
};
var countryList = new List()
{
new Country{CountryCode="US", CountryName="America"},
new Country{CountryCode="IN", CountryName="India"}
};
3. Now we are going to do join between two tables on group by country code.
var resultGroup = countryList.GroupJoin(customerList, a=>a.CountryCode, b=>b.CountryCode,(a,b) => new
{
a.CountryCode,
a.CountryName,
CustomerList = b.ToList()
}).ToList();
Here we will get the distinct lint of all the countries.

In the customer list we will get the list of all the customers belonging to that country.

Please comment and share if you like this post and tell us about how we can enhance our posts. Thanks.
Subhajit
This is Subhajit Sinha, with more than 8 years of experience as a full stack developer in renowned Multinational Companies, here in this portal as an online tutor I will guide you personally to level up your programming skills and also will coach you to augment your career and salary as well.
Search
Popular posts

Comments (2)
Alhassen Brini
Although the conte t is understandable, however, the web site is not responsive on my phone.
Subhajit
@ALHASSEN, Thanks for pointing out this issue, actually the coding part is non responsive, we are working on it, however for the time being, in the mobile browser, in the settings, there is an option for desktop view, you can check the full coding in that mode. Again, thanks a lot for pointing out this issue.
Comments are closed.