使用Linq语句执行外连接

时间:2013-01-16 10:33:04

标签: linq lambda outer-join

从下面的集合开始,我需要返回一个满足测试结果集的Linq语句?

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[] { }));
}

注意:这是上一个问题的后续跟进:Convert Lambda into Linq Statement with nested for loop

0 个答案:

没有答案