0

Starting with the collection below, what Linq statement do I need to return a results set that satisfies the test?

private List<dynamic> _results;

[SetUp]
public void SetUp()
{
    _results = new List<dynamic>
    {
        new {Id = 1, Names = new[] {"n1"}, Tags = new[] {"abc", "def"}},
        new {Id = 2, Names = new[] {"n2", "n3"}, Tags = new[] {"ghi"}},
        new {Id = 3, Names = new[] {"n1", "n3"}, Tags = new[] {"def", "xyz"}},
        new {Id = 4, Names = new[] {"n4"}, Tags = new string[] {}}
    };
}

private ILookup<string, string> GetOuterJoinedCollection(IEnumerable<dynamic> results)
{
    // ???
}

[Test]
public void Test()
{
    ILookup<string, string> list = GetOuterJoinedCollection(_results);

    Assert.That(list.Count, Is.EqualTo(4));
    Assert.That(list["n1"], Is.EquivalentTo(new [] { "abc", "def", "def", "xyz" }));
    Assert.That(list["n2"], Is.EquivalentTo(new [] { "ghi" }));
    Assert.That(list["n3"], Is.EquivalentTo(new [] { "ghi", "def", "xyz" }));
    Assert.That(list["n4"], Is.EquivalentTo(new string[] { }));
}

Note: this is a follow up from a previous question: Convert Lambda into Linq Statement with nested for loop

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nick
  • 6,366
  • 5
  • 43
  • 62
  • Do you want a left outer join, a right outer join or a full outer join? – Daniel Hilgarth Jan 16 '13 at 10:34
  • I want every `Name` property value in the list and its associated `Tag` property values, even if the `Name` has no associated `Tag`s. I think this is either a left or right outer join. – Nick Jan 16 '13 at 10:48
  • In your previous question you can just change ToDictionary in ToLookup. – Gert Arnold Jan 17 '13 at 08:24

0 Answers0