Left outer join return all element of the first collection,does not matter whether it has any correlated elements in the second collection.You can implement LINQ left outer join by applying the DefaultIfEmpty method on the results of a group join.
Here is the example of left outer join using LINQ.
Example
---------------------------------------------------
Output :
Here is the example of left outer join using Group join.
Example
----------------------------------------------
Here is the example of left outer join using LINQ.
Example
---------------------------------------------------
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
a in
students
join
b in
students2 on
a.roll equals
b.roll into
dep
from
c in
dep.DefaultIfEmpty()
select
new
{
a.name,
a.roll,
a.age,
c.address
};
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} Roll : {1} Age:{2} Address : {3}",
obj.name, obj.roll, obj.age, obj.address);
}
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 Roll : 123 Age:12 Address : 12,ABC Road
Name :John2 Roll : 124 Age:13 Address : 13,ABC Road
Name :John3 Roll : 125 Age:14 Address : 14,ABC Road
Name :John4 Roll : 126 Age:15 Address : 15,ABC Road
Name :John2 Roll : 124 Age:13 Address : 13,ABC Road
Name :John3 Roll : 125 Age:14 Address : 14,ABC Road
Name :John4 Roll : 126 Age:15 Address : 15,ABC Road
Here is the example of left outer join using Group join.
Example
----------------------------------------------
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 = students.GroupJoin(students2, lang => lang.roll, pers =>
pers.roll,
(lang,
ps) => new
{ Name = lang.name, Roll = lang.roll, Age = lang.age, Persons = ps
});
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} ",
obj.Name);
}
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
Name :John2
Name :John3
Name :John4
Name :John5
Name :John6
Name :John7
Name :John2
Name :John3
Name :John4
Name :John5
Name :John6
Name :John7
No comments:
Post a Comment