Use a Linq statement to perform outer join

泄露秘密 提交于 2020-01-06 19:42:31

问题


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

来源:https://stackoverflow.com/questions/14356327/use-a-linq-statement-to-perform-outer-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!