The Join operator join more than one collection and return a new collection that contains elements supplied collections that satisfies specified expression.
Below is the example of LINQ Join using LINQ query.
---------------------------------------------
using System;
Below is the example of LINQ Join using LINQ query.
---------------------------------------------
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},
new
student2{address
= "13,ABC
Road",roll=124},
new
student2{address
= "14,ABC
Road",roll=125},
new
student2{address
= "15,ABC
Road",roll=131},
new
student2{address
= "16,ABC
Road",roll=132},
new
student2{address
= "17,ABC
Road",roll=133},
new
student2{address
= "18,ABC
Road",roll=134}
};
var
query = from
a in
students
join
b in
students2 on
a.roll equals
b.roll
select
new
{ a.name,a.roll,b.address };
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} Roll : {1} Address : {2}",
obj.name, obj.roll, 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;
}
}
Name :John1 Roll : 123 Address : 12,ABC Road
Name :John2 Roll : 124 Address : 13,ABC Road
Name :John3 Roll : 125 Address : 14,ABC Road
------------------------------------------------------------
Name :John2 Roll : 124 Address : 13,ABC Road
Name :John3 Roll : 125 Address : 14,ABC Road
------------------------------------------------------------
Below is the example of LINQ Join using lambda expression.
-------------------------------------------------------------------
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},
new
student2{address
= "13,ABC
Road",roll=124},
new
student2{address
= "14,ABC
Road",roll=125},
new
student2{address
= "15,ABC
Road",roll=131},
new
student2{address
= "16,ABC
Road",roll=132},
new
student2{address
= "17,ABC
Road",roll=133},
new
student2{address
= "18,ABC
Road",roll=134}
};
var
query = students.Join(students2,
a
=> a.roll,
b
=> b.roll,
(a,
b) => new
{ a.name, a.roll, b.address });
foreach
(var
obj in
query)
{
Console.WriteLine("Name
:{0} Roll : {1} Address : {2}",
obj.name, obj.roll, 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;
}
}
Name :John2 Roll : 124 Address : 13,ABC Road
Name :John3 Roll : 125 Address : 14,ABC Road
----------------------------------------------------------
No comments:
Post a Comment