Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,32 @@
Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Left outer join - LeftJoin method");
Console.WriteLine();

results = Utilities.PerformLeftJoinWithLeftJoinMethod(Utilities.Songs, Utilities.Authors);

foreach (var item in results)
{
Console.WriteLine($"Title: {item.Title}, Author: {item.AuthorName}");
}

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Right outer join - RightJoin method");
Console.WriteLine();

results = Utilities.PerformRightJoinWithRightJoinMethod(Utilities.Songs, Utilities.Authors);

foreach (var item in results)
{
Console.WriteLine($"Title: {item.Title}, Author: {item.AuthorName}");
}

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("Full outer join");
Console.WriteLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ internal class SongWithAuthorComparer : IEqualityComparer<SongWithAuthor>
{
public bool Equals(SongWithAuthor? x, SongWithAuthor? y)
{
if (x?.AuthorName == y?.AuthorName && x?.Title == y?.Title)
{
return true;
}

return false;
return x?.AuthorName == y?.AuthorName && x?.Title == y?.Title;
}

public int GetHashCode([DisallowNull] SongWithAuthor obj)
{
return obj.AuthorName.GetHashCode();
return HashCode.Combine(obj.Title, obj.AuthorName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ public static List<SongWithAuthor> PerformRightJoinWithMethodSyntax(

return results.ToList();
}

public static List<SongWithAuthor> PerformLeftJoinWithLeftJoinMethod(
List<Song> songs, List<Author> authors)
{
var results = songs.LeftJoin(
authors,
song => song.AuthorId,
author => author.Id,
(song, author) => new SongWithAuthor
{
Title = song.Title,
AuthorName = author?.Name ?? "unknown"
});

return results.ToList();
}

public static List<SongWithAuthor> PerformRightJoinWithRightJoinMethod(
List<Song> songs, List<Author> authors)
{
var results = songs.RightJoin(
authors,
song => song.AuthorId,
author => author.Id,
(song, author) => new SongWithAuthor
{
Title = song?.Title ?? "-",
AuthorName = author.Name
});

return results.ToList();
}

public static List<SongWithAuthor> PerformFullOuterJoin(
List<Song> songs, List<Author> authors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,56 @@ public void GivenTwoDataSources_WhenPerformRightJoinWithMethodSyntaxMethodCalled
Assert.Equal(authors.Count, output);
}

[Fact]
public void GivenTwoDataSources_WhenPerformLeftJoinWithLeftJoinMethodCalled_ThenAllItemsFromLeftSourceAreReturned()
{
// Arrange
List<Song> songs =
[
new() { Id = 1, Title = "song A", AuthorId = 3 },
new() { Id = 2, Title = "song B", AuthorId = 1 },
new() { Id = 3, Title = "song C", AuthorId = 5 },
new() { Id = 4, Title = "song D", AuthorId = 2 }
];

List<Author> authors =
[
new() { Id = 1, Name = "Author A" },
new() { Id = 9, Name = "Author B" }
];

// Act
var output = Utilities.PerformLeftJoinWithLeftJoinMethod(songs, authors).Count;

// Assert
Assert.Equal(songs.Count, output);
}

[Fact]
public void GivenTwoDataSources_WhenPerformRightJoinWithRightJoinMethodCalled_ThenAllItemsFromRightSourceAreReturned()
{
// Arrange
List<Song> songs =
[
new() { Id = 1, Title = "song A", AuthorId = 3 },
new() { Id = 2, Title = "song B", AuthorId = 1 },
new() { Id = 3, Title = "song C", AuthorId = 5 },
new() { Id = 4, Title = "song D", AuthorId = 2 }
];

List<Author> authors =
[
new() { Id = 1, Name = "Author A" },
new() { Id = 9, Name = "Author B" }
];

// Act
var output = Utilities.PerformRightJoinWithRightJoinMethod(songs, authors).Count;

// Assert
Assert.Equal(authors.Count, output);
}

[Fact]
public void GivenTwoDataSources_WhenPerformFullOuterJoinMethodCalled_ThenAllItemsFromBothSourcesAreReturned()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading