linq Right Outer Joins
linq Right Outer Joins
Join when using LINQ. There are no keywords defined in C#, we have to use DefaultIfEmpty() function to get the desired result.private void RightOuterJoins() { Tag = "Right Outer Joins"; Person bhaumik = new Person { FirstName = "Bhaumik", LastName = "Patel" }; Person hardik = new Person { FirstName = "Hardik", LastName = "Patel" }; Person manan = new Person { FirstName = "Manan", LastName = "Bhavsar" }; Person vishal = new Person { FirstName = "Vishal", LastName = "Gorasia" }; Pet patel = new Pet { Name = "Patel", Owner = hardik }; Pet hardik1 = new Pet { Name = "Hardik", Owner = hardik }; Pet sonu = new Pet { Name = "Sonu", Owner = manan }; Pet gorasia = new Pet { Name = "Gorasia", Owner = hardik }; Pet patel1 = new Pet { Name = "Patel", Owner = bhaumik }; // Create two lists. List<Person> people = new List<Person> { bhaumik, hardik, manan, vishal }; List<Pet> pets = new List<Pet> { patel, hardik1, sonu, gorasia, patel1 }; var 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), }; grdView.DataSource = rightOuterJoin.ToList(); grdView.DataBind(); }
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; } }
SEE ALL LINQ JOINS
Download All Linq Joins
linq Right Outer Joins
Reviewed by Bhaumik Patel
on
8:15 PM
Rating: