Left outer join return all element of the second collection,does not matter whether it has any correlated elements in the first collection.You can implement LINQ right outer join by applying the DefaultIfEmpty method on the results of a group join.
Here is the exmaple
----------------------------------------------
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
var
students = new
student[]
{
new
student{name
= "John1",roll=123,age=12},
new
student{name
= "John2",roll=124,age=13},
new
student{name
= "John3",roll=125,age=14},
new
student{name
= "John4",roll=126,age=15},
new
student{name
= "John5",roll=127,age=16},
new
student{name
= "John6",roll=128,age=17},
new
student{name
= "John7",roll=129,age=18}
};
var
students2 = new
student2[]
{
new
student2{address
= "12,ABC
Road",roll=123,age=12},
new
student2{address
= "13,ABC
Road",roll=124,age=13},
new
student2{address
= "14,ABC
Road",roll=125,age=14},
new
student2{address
= "15,ABC
Road",roll=126,age=15},
new
student2{address
= "16,ABC
Road",roll=132,age=16},
new
student2{address
= "17,ABC
Road",roll=133,age=17},
new
student2{address
= "18,ABC
Road",roll=134,age=17}
};
var
query = from
proj in
students
join
employee in
students2
on
proj.roll equals
employee.roll into
joinDeptEmp
from
employee in
joinDeptEmp.DefaultIfEmpty()
select
new
{
name
= proj.name,
address
= employee.address,
age
= proj.age,
roll
= proj.roll
};
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} ,Age :{0},Roll :{0},Address :{0}",
obj.name, obj.address, obj.age, obj.roll);
}
Console.ReadKey();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
public
class
student2
{
public
string
address { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
Output :
Name :John1 ,Age :John1,Roll :John1,Address :John1
Name :John2 ,Age :John2,Roll :John2,Address :John2
Name :John3 ,Age :John3,Roll :John3,Address :John3
Name :John4 ,Age :John4,Roll :John4,Address :John4
Name :John2 ,Age :John2,Roll :John2,Address :John2
Name :John3 ,Age :John3,Roll :John3,Address :John3
Name :John4 ,Age :John4,Roll :John4,Address :John4
No comments:
Post a Comment