Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Join method #109

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions ShittyLINQ/Join.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;

namespace ShittyLINQ
{
public static partial class Extensions
{
public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IEnumerable<TOuter> outer,
IEnumerable<TInner> inner,
Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector,
Func<TOuter, TInner, TResult> resultSelector)
{
if (outer == null || outerKeySelector == null || innerKeySelector == null || resultSelector == null) throw new ArgumentNullException();
var outerIterator = outer.GetEnumerator();

var comparer = EqualityComparer<TKey>.Default;
TOuter currentOuter = outerIterator.Current;

while (outerIterator.MoveNext())
{
currentOuter = outerIterator.Current;
TInner currentInner;
var innerIterator = inner.GetEnumerator();

// Find match from inner
while (innerIterator.MoveNext())
{
currentInner = innerIterator.Current;
if (comparer.Equals(outerKeySelector(currentOuter), innerKeySelector(currentInner)))
{
yield return resultSelector(currentOuter, currentInner);
}
}
}
}
}
}
61 changes: 61 additions & 0 deletions ShittyLinqTests/JoinTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShittyLINQ;
using System;
using System.Collections.Generic;

namespace ShittyTests
{
[TestClass]
public class JoinTests
{
class Person
{
public string Name { get; set; }
}

class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}

[TestMethod]
public void Join_PerformsJoinOperation()
{
Person magnus = new Person { Name = "Hedlund, Magnus" };
Person terry = new Person { Name = "Adams, Terry" };
Person charlotte = new Person { Name = "Weiss, Charlotte" };

Pet barley = new Pet { Name = "Barley", Owner = terry };
Pet boots = new Pet { Name = "Boots", Owner = terry };
Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
Pet daisy = new Pet { Name = "Daisy", Owner = magnus };

List<Person> people = new List<Person> { magnus, terry, charlotte };
List<Pet> pets = new List<Pet> { barley, boots, whiskers, daisy };

// Create a list of Person-Pet pairs where
// each element is an anonymous type that contains a
// Pet's name and the name of the Person that owns the Pet.
var query =
people.Join(pets,
person => person,
pet => pet.Owner,
(person, pet) =>
new { OwnerName = person.Name, Pet = pet.Name })
.ToList();

Assert.AreEqual("Hedlund, Magnus", query[0].OwnerName);
Assert.AreEqual("Daisy", query[0].Pet);

Assert.AreEqual("Adams, Terry", query[1].OwnerName);
Assert.AreEqual("Barley", query[1].Pet);

Assert.AreEqual("Adams, Terry", query[2].OwnerName);
Assert.AreEqual("Boots", query[2].Pet);

Assert.AreEqual("Weiss, Charlotte", query[3].OwnerName);
Assert.AreEqual("Whiskers", query[3].Pet);
}
}
}