linq joins
Join In Linq
Join when using LINQ. There are no keywords defined in C#, we have to use DefaultIfEmpty() function to get the desired result.linq simple outer joins
var query = from person in people
join pet in pets on person.FirstName equals pet.Name
select new { person.FirstName, person.LastName };
linq outer Joinslinq inner joins
var query = from person in people
join pet in pets on person equals pet.Owner
select new { OwnerName = person.FirstName, PetName = pet.Name };
linq left outer joins
var query = from person in people
join pet in pets on person equals pet.Owner into gj
from subpet in gj.DefaultIfEmpty()
select new { person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name) };
linq right outer joinsvar rightOuterJoin = from pet in pets
join person in people on pet.Name equals person.FirstName into temp
from person in temp.DefaultIfEmpty()
select new
{
PatName = pet.Name,
FirstName = person != null ? person.FirstName : default(string), // person.FirstName,
LastName = person != null ? person.LastName : default(string),
};
Person and Pet Class
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
Download All Linq Joins
linq joins
Reviewed by Bhaumik Patel
on
7:37 PM
Rating: