Group join in lambda C#


Group join in lambda C#

  1. 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<Customer>()
    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<Country>()
        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
        CustomerList = b.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.

Comments (2)

  1. Alhassen Brini

    Although the conte t is understandable, however, the web site is not responsive on my phone.

    1. @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.