Left Join
Jasurbek Xasanboyev
class Program
{
static void Main(string[] args)
{
var students = new List<Student>()
{
new Student() { Id = 1, Name = "Jasurbek", AddressId = 1},
new Student() { Id = 2, Name = "Xondamir", AddressId = 5},
new Student() { Id = 3, Name = "Shoxruh", AddressId = 3},
new Student() { Id = 4, Name = "Shaxzod", AddressId = 4},
new Student() { Id = 5, Name = "Abdulloh"}
};
var addresses = new List<Address>()
{
new Address() {Id = 1, AddressLine = "Line 1"},
new Address() {Id = 2, AddressLine = "Line 2"},
new Address() {Id = 3, AddressLine = "Line 3"},
new Address() {Id = 4, AddressLine = "Line 4"},
};
var QueerySyntax = from std in students
join add in addresses on
std.AddressId equals add.Id into
stdAddress
from studentAddress in stdAddress.DefaultIfEmpty()
select new { Name = std.Name, StudentAddress = studentAddress != null ? studentAddress.AddressLine : "Empty"};
foreach (var item in QueerySyntax)
{
Console.WriteLine($"Name: {item.Name} Line: {item.StudentAddress}");
}
/*OUTPUT: Name: Jasurbek Line: Line 1
Name: Xondamir Line: Empty
Name: Shoxruh Line: Line 3
Name: Shaxzod Line: Line 4
Name: Abdulloh Line: Empty */
}
}
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int AddressId { get; set; }
}
class Address
{
public int Id { get; set; }
public string AddressLine { get; set; }
}Last updated
Was this helpful?