C# LINQ JOIN Operator

Updated on     Kisan Patel

The Join operators in LINQ are used join objects in one data source with objects that share a common attribute in another data source.

The JOIN and GROUPJOIN clauses act as the Join operators in LINQ.

The JOIN Opearator

The JOIN clause implements an inner join, meaning those objects that have a match in other data set are returned. You should be having two lists of data that you need to join using the JOIN clause.

For Example,

NorthwindEntities _db = new NorthwindEntities();

var customers = from c in _db.Customers
                join o in _db.Orders on c.CustomerID equals o.CustomerID
                select new {c.ContactTitle, o.OrderDate};

foreach (var customer in customers)
{
    Console.WriteLine(customer.ContactTitle + " - " + customer.OrderDate);
}

..would translate to..

 var customers = _db.Customers.Join(
                                _db.Orders,
                                c => c.CustomerID,
                                o => o.CustomerID,
                                (c, o) => new { c, o })
                                .Select(x => new {x.c.ContactTitle, x.o.OrderDate });

The output of the above C# code is…

linq-join-example


C# LINQ

Leave a Reply