Joining in LINQ

Joining in LINQ is similar to using JOIN operators in LINQ. There are two types of operator that perform JOIN functions in LINQ: Join and GroupJoin operators. These operators are used to combine two input sequence based on a certain condition and return the output as one sequence. The Join operator returns the sequence in the flat form while GroupJoin returns the sequence in hierarchical form.

Join Operator

Have a look at the example of Join operators.

´╗┐using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpTutorials {

    public class Customer {
        public int custId;
        public string custname;

        public Customer(int cid, string cname) {
            this.custId = cid;
            this.custname = cname;
        }
    }

    public class Order {
        public int custId;
        public string itemname;

        public Order(int cid, string iname) {
            this.custId = cid;
            this.itemname = iname;
        }
    }

    class Tutorial {
        static void Main() {
            List<Customer> customercollection = new List<Customer>();

            Customer c1 = new Customer(2, "John");
            Customer c2 = new Customer(3, "Mike");
            Customer c3 = new Customer(4, "Sara");
            Customer c4 = new Customer(5, "Kelly");
            Customer c5 = new Customer(6, "Elizabeth");

            customercollection.Add(c1);
            customercollection.Add(c2);
            customercollection.Add(c3);
            customercollection.Add(c4);
            customercollection.Add(c5);

            List<Order> ordercollection = new List<Order>();
            Order o1 = new Order(2, "Laptop");
            Order o2 = new Order(2, "LCD");
            Order o3 = new Order(4, "Furniture");
            Order o4 = new Order(6, "Headseat");
            Order o5 = new Order(6, "Standing Mic");

            ordercollection.Add(o1);
            ordercollection.Add(o2);
            ordercollection.Add(o3);
            ordercollection.Add(o4);
            ordercollection.Add(o5);

            IEnumerable<string> reports = from c in customercollection
                                          join o in ordercollection
                                          on c.custId equals o.custId
                                          select c.custname + " purchased " + o.itemname;
            foreach (string repline in reports) Console.WriteLine(repline);
            Console.ReadKey();
        }
    }
}

Download the code

In the above code, collection of two classes Customer and Order have been created. Both the classes have a custId field. This is the field on which the Join will be applied and the name of the Customer and Item will be returned as a single sequence. This is similar to full Join in SQL

GroupJoin

GroupJoin returns joined query in hierarchical form, which means that the customer name will be displayed first, followed by all his purchases and then the next customer name will be displayed. The GroupJoin query is similar to Join query, except into operator is used before Select. Have a look at the following example.

´╗┐using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpTutorials {

    public class Customer {
        public int custId;
        public string custname;

        public Customer(int cid, string cname) {
            this.custId = cid;
            this.custname = cname;
        }
    }

    public class Order {
        public int custId;
        public string itemname;

        public Order(int cid, string iname) {
            this.custId = cid;
            this.itemname = iname;
        }
    }

    class Tutorial {
        static void Main() {
            List<Customer> customercollection = new List<Customer>();

            Customer c1 = new Customer(2, "John");
            Customer c2 = new Customer(3, "Mike");
            Customer c3 = new Customer(4, "Sara");
            Customer c4 = new Customer(5, "Kelly");
            Customer c5 = new Customer(6, "Elizabeth");

            customercollection.Add(c1);
            customercollection.Add(c2);
            customercollection.Add(c3);
            customercollection.Add(c4);
            customercollection.Add(c5);

            List<Order> ordercollection = new List<Order>();
            Order o1 = new Order(2, "Laptop");
            Order o2 = new Order(2, "LCD");
            Order o3 = new Order(4, "Furniture");
            Order o4 = new Order(6, "Headseat");
            Order o5 = new Order(6, "Standing Mic");

            ordercollection.Add(o1);
            ordercollection.Add(o2);
            ordercollection.Add(o3);
            ordercollection.Add(o4);
            ordercollection.Add(o5);

            var reports = from c in customercollection
                                          join o in ordercollection
                                          on c.custId equals o.custId
                                          into customerpurchases
                                          select new { CustomerName = c.custname, customerpurchases };
            foreach (var custname in reports) {
                if (custname.customerpurchases.Count() > 0) {
                    Console.WriteLine(custname.CustomerName + " purchased ");
                    foreach (var item in custname.customerpurchases) {
                        Console.WriteLine(item.itemname);

                    }
                }
            }
            Console.ReadKey();
        }
    }
}

Download the code

<<< Grouping Data via GroupBy in LINQOrdering in LINQ >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .