I would like to zip the items of 2 sequences based on a common property similar to joining them when using enumerables. How can I make the second test pass?
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
public class SequenceTests
{
private class Entry
{
public Entry(DateTime timestamp, string value)
{
Timestamp = timestamp;
Value = value;
}
public DateTime Timestamp { get; }
public string Value { get; }
}
private readonly IEnumerable<Entry> Tasks = new List<Entry>
{
new Entry(new DateTime(2021, 6, 6), "Do homework"),
new Entry(new DateTime(2021, 6, 7), "Buy groceries"), // <-- This date is also in the People collection!
new Entry(new DateTime(2021, 6, 8), "Walk the dog"),
};
private readonly IEnumerable<Entry> People = new List<Entry>
{
new Entry(new DateTime(2021, 6, 4), "Peter"),
new Entry(new DateTime(2021, 6, 5), "Jane"),
new Entry(new DateTime(2021, 6, 7), "Paul"), // <-- This date is also in the Tasks collection!
new Entry(new DateTime(2021, 6, 9), "Mary"),
};
private class Assignment
{
public string Task { get; set; }
public string Person { get; set; }
}
[Test]
public void Join_two_collections_should_succeed()
{
var assignments = Tasks
.Join(People,
task => task.Timestamp,
person => person.Timestamp,
(task, person) => new Assignment { Task = task.Value, Person = person.Value });
Assert.AreEqual(1, assignments.Count());
Assert.AreEqual("Buy groceries", assignments.First().Task);
Assert.AreEqual("Paul", assignments.First().Person);
}
[Test]
public async Task Zip_two_sequences_should_succeed()
{
var tasks = Observable.ToObservable(Tasks);
var people = Observable.ToObservable(People);
var sequence = tasks
.Zip(people)
.Select(pair => new Assignment { Task = pair.First.Value, Person = pair.Second.Value });
var assignments = await sequence.ToList();
Assert.AreEqual(1, assignments.Count);
Assert.AreEqual("Buy groceries", assignments.First().Task);
Assert.AreEqual("Paul", assignments.First().Person);
}
}