Today I came across a LINQ method, DefaultIfEmpty(), which is quite similar to the Left Join of SQL.
DefaultIfEmpty works like a left join and gives all the records from the left table including the matching records from the right table. Use DefaultIfEmpty<TSource>(IEnumerable<TSource>) to provide a default value in case the source sequence is empty.
For more information about DefaultIfEmpty(), please have a look into this link: http://msdn.microsoft.com/en-us/library/bb397895.aspx
Kindly look into the code given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQ_IfByDefault
{
//Class Employee having two properties
class Employee
{
public string Name { get; set; }
public string EmpID { get; set; }
}
//Class Worker having two properties
class Worker
{
public string WId { get; set; }
public string City { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Object Initialization for Employee class
List<Employee> objEmployee = new List<Employee>{
new Employee{ Name="Sachin",EmpID="I001"},
new Employee{ Name="Vijay",EmpID="I002"},



Comments
Join the conversation! Your thoughts help the community grow.