public class TableA
{
public string Name { get; set; }
public string Pet{ get; set; }
}
public class TableB
{
public string Name { get; set; }
public string TransportationType { get; set; }
}
Public Class TableA
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Pet() As String
Get
Return m_Pet
End Get
Set(value As String)
m_Pet = Value
End Set
End Property
Private m_Pet As String
End Class
Public Class TableB
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property TransportationType() As String
Get
Return m_TransportationType
End Get
Set(value As String)
m_TransportationType = Value
End Set
End Property
Private m_TransportationType As String
End Class
SELECT *
FROM TableA A
INNER JOIN TableB B
ON A.Name = B.Name
static void InnerJoin(List<TableA> TableA,List<TableB> TableB)
{
var query = from a in TableA
join b in TableB
on a.Name equals b.Name
select new {a.Name,a.Pet,b.TransportationType };
foreach(var person in query)
{
Console.WriteLine(person);
}
}
Private Shared Sub InnerJoin(TableA As List(Of TableA), TableB As List(Of TableB))
Dim query = From a In TableA _
Join b In TableB _
On a.Name Equals b.Name
Select New With
{ _
a.Name, _
a.Pet, _
b.TransportationType _
}
For Each person In query
Console.WriteLine(person)
Next
End Sub
SELECT *
FROM TableA A
LEFT OUTER JOIN Table B
ON A.Name = B.Name
static void LeftOuterJoin(List<TableA> TableA, List<TableB> TableB)
{
TableB DefaultTableB = new TableB { Name = null, TransportationType = null };
var query = from a in TableA
join b in TableB
on a.Name equals b.Name
into temp
from b in temp.DefaultIfEmpty(DefaultTableB)
select new
{
a.Name,
a.Pet,
TransportationType = b.TransportationType
};
foreach (var person in query)
{
Console.WriteLine(person);
}
}
Private Shared Sub LeftOuterJoin(TableA As List(Of TableA), TableB As List(Of TableB))
Dim DefaultTableB As New TableB() With { _
.Name = Nothing, _
.TransportationType = Nothing _
}
Dim query = From a In TableA _
Group Join b In TableB _
On a.Name Equals b.Name Into temp = Group _
From b In temp.DefaultIfEmpty(DefaultTableB) _
Select New With { _
a.Name, _
a.Pet, _
.TransportationType = b.TransportationType _
}
For Each person In query
Console.WriteLine(person)
Next
End Sub
SELECT *
FROM TableA A
FULL OUTER JOIN TableB B
ON A.Name = B.Name
static void FullOuterJoin(List<TableA> TableA, List<TableB> TableB)
{
TableA DefaultTableA = new TableA { Name=null, Pet = null };
TableB DefaultTableB = new TableB { Name = null, TransportationType = null };
var leftOuterJoin = from a in TableA
join b in TableB
on a.Name equals b.Name
into temp
from b in temp.DefaultIfEmpty(DefaultTableB)
select new
{
a.Name,
a.Pet,
TransportationType=b.TransportationType
};
var rightOuterJoin = from b in TableB
join a in TableA
on b.Name equals a.Name
into temp
from a in temp.DefaultIfEmpty(DefaultTableA)
select new
{
b.Name,
a.Pet,
TransportationType = b.TransportationType
};
var query = leftOuterJoin.Union(rightOuterJoin);
foreach (var person in query)
{
Console.WriteLine(person);
}
}
Private Shared Sub FullOuterJoin(TableA As List(Of TableA), TableB As List(Of TableB))
Dim DefaultTableA As New TableA() With { _
.Name = Nothing, _
.Pet = Nothing _
}
Dim DefaultTableB As New TableB() With { _
.Name = Nothing, _
.TransportationType = Nothing _
}
Dim leftOuterJoin = From a In TableA _
Group Join b In TableB _
On a.Name Equals b.Name Into temp = Group _
From b In temp.DefaultIfEmpty(DefaultTableB) _
Select New With { _
a.Name, _
a.Pet, _
.TransportationType = b.TransportationType _
}
Dim rightOuterJoin = From b In TableB _
Group Join a In TableA _
On b.Name Equals a.Name Into temp = Group _
From a In temp.DefaultIfEmpty(DefaultTableA) _
Select New With { _
b.Name, _
a.Pet, _
.TransportationType = b.TransportationType _
}
Dim query = (From q In leftOuterJoin.Union(rightOuterJoin) _
Select q.Name, q.Pet, q.TransportationType).Distinct
For Each person In query
Console.WriteLine(person)
Next
End Sub
SELECT *
FROM TableA A
LEFT OUTER JOIN TableB B
ON A.Name = B.Name
WHERE B.Name IS NULL
static void ExcludeTableB(List<TableA> TableA, List<TableB> TableB)
{
TableB DefaultTableB = new TableB { Name = null, TransportationType = null };
var query = from a in TableA
join b in TableB
on a.Name equals b.Name
into temp
from b in temp.DefaultIfEmpty(DefaultTableB)
where b.Name==null
select new
{
a.Name,
a.Pet,
TransportationType = b.TransportationType
};
foreach (var person in query)
{
Console.WriteLine(person);
}
}
Private Shared Sub ExcludeTableB(TableA As List(Of TableA), TableB As List(Of TableB))
Dim DefaultTableB As New TableB() With { _
.Name = Nothing, _
.TransportationType = Nothing _
}
Dim query = From a In TableA _
Group Join b In TableB _
On a.Name Equals b.Name Into temp = Group _
From b In temp.DefaultIfEmpty(DefaultTableB) _
Where b.Name Is Nothing
Select New With { _
a.Name, _
a.Pet, _
.TransportationType = b.TransportationType _
}
For Each person In query
Console.WriteLine(person)
Next
End Sub
SELECT *
FROM TableA A
FULL OUTER JOIN TableB B
ON A.Name = B.Name
WHERE B.Name IS NULL
OR A.Name IS NULL
static void ExcludeMatches(List<TableA> TableA, List<TableB> TableB)
{
TableA DefaultTableA = new TableA { Name = null, Pet = null };
TableB DefaultTableB = new TableB { Name = null, TransportationType = null };
var leftOuterJoin = from a in TableA
join b in TableB
on a.Name equals b.Name
into temp
from b in temp.DefaultIfEmpty(DefaultTableB)
select new
{
NameA = a.Name,
a.Pet,
NameB = b.Name,
TransportationType = b.TransportationType
};
var rightOuterJoin = from b in TableB
join a in TableA
on b.Name equals a.Name
into temp
from a in temp.DefaultIfEmpty(DefaultTableA)
select new
{
NameA = a.Name,
a.Pet,
NameB = b.Name,
TransportationType = b.TransportationType
};
var query = from q in leftOuterJoin.Union(rightOuterJoin)
where (q.NameA ==null|| q.NameB==null)
select q;
foreach (var person in query)
{
Console.WriteLine(person);
}
}
Private Shared Sub ExcludeMatches(TableA As List(Of TableA), TableB As List(Of TableB))
Dim DefaultTableA As New TableA() With { _
.Name = Nothing, _
.Pet = Nothing _
}
Dim DefaultTableB As New TableB() With { _
.Name = Nothing, _
.TransportationType = Nothing _
}
Dim leftOuterJoin = From a In TableA _
Group Join b In TableB _
On a.Name Equals b.Name Into temp = Group _
From b In temp.DefaultIfEmpty(DefaultTableB) _
Select New With { _
.NameA = a.Name, _
a.Pet, _
.NameB = b.Name, _
.TransportationType = b.TransportationType _
}
Dim rightOuterJoin = From b In TableB _
Group Join a In TableA _
On b.Name Equals a.Name Into temp = Group _
From a In temp.DefaultIfEmpty(DefaultTableA) _
Select New With { _
.NameA = a.Name, _
a.Pet, _
.NameB = b.Name, _
.TransportationType = b.TransportationType _
}
Dim query = (From q In leftOuterJoin.Union(rightOuterJoin) _
Where q.NameA Is Nothing Or q.NameB Is Nothing
Select q.NameA, q.Pet, q.NameB, q.TransportationType).Distinct
For Each person In query
Console.WriteLine(person)
Next
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
Having the SQL syntax bridge the LINQ syntax easier to understand.
Thanks for the write up!
Author
Commented:Commented:
Commented: