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
32 changes: 32 additions & 0 deletions ShittyLINQ/Intersect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace ShittyLINQ
{
using System;
using System.Collections.Generic;

public static partial class Extensions
{
public static IEnumerable<T> Intersect<T>(this IEnumerable<T> self, IEnumerable<T> second)
{
return Intersect(self, second, EqualityComparer<T>.Default);
}

public static IEnumerable<T> Intersect<T>(this IEnumerable<T> self, IEnumerable<T> second, IEqualityComparer<T> comparer)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
if (second == null)
throw new ArgumentNullException(nameof(second));

foreach (var item in self)
{
foreach (var secondItem in second)
{
if (comparer.Equals(item, secondItem))
{
yield return item;
}
}
}
}
}
}
58 changes: 58 additions & 0 deletions ShittyLinqTests/IntersectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace ShittyTests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShittyLINQ;
using ShittyTests.TestHelpers;

[TestClass]
public class IntersectTests
{
[TestMethod]
public void Intersect_ReturnsExpected()
{
var first = new[] { 1, 2, 3, 4, 5 };
var second = new[] { 1, 2, 5, 6, 7 };
var expectedResult = new[] { 1, 2, 5 };

var result = first.Intersect(second);

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}

[TestMethod]
public void Intersect_ReturnsExpectedWithFirstCollectionEmpty()
{
var first = new int[] { };
var second = new[] { 1, 2, 5, 6, 7 };
var expectedResult = new int[] { };

var result = first.Intersect(second);

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}

[TestMethod]
public void Intersect_ReturnsExpectedWithSecondCollectionEmpty()
{
var first = new[] { 1, 2, 3, 4, 5 };
var second = new int[] { };
var expectedResult = new int[] { };

var result = first.Intersect(second);

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}

[TestMethod]
public void Intersect_ReturnsExpectedWithoutCommonValue()
{
var first = new[] { 1, 2, 3, 4, 5 };
var second = new int[] { 6, 7, 8, 9, 10 };
var expectedResult = new int[] { };

var result = first.Intersect(second);

TestHelper.AssertCollectionsAreSame(expectedResult, result);
}
}
}