使用Linq语句执行外部联接
从下面的集合开始,我需要什么Linq语句来返回满足测试要求的结果集使用Linq语句执行外部联接,linq,lambda,outer-join,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 =
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[] { }));
}
private List\u结果;
[设置]
公共作废设置()
{
_结果=新列表
{
新{Id=1,name=new[]{“n1”},Tags=new[]{“abc”,“def”},
新的{Id=2,name=new[]{“n2”,“n3”},Tags=new[]{“ghi”},
新{Id=3,name=new[]{“n1”,“n3”},Tags=new[]{“def”,“xyz”},
新{Id=4,name=new[]{“n4”},Tags=newstring[]{}
};
}
私有ILookup GetOuterJoinedCollection(IEnumerable结果)
{
// ???
}
[测试]
公开无效测试()
{
ILookup list=GetOuterJoinedCollection(_结果);
Assert.That(list.Count,Is.EqualTo(4));
Assert.That(list[“n1”],等于(new[]{“abc”,“def”,“def”,“xyz”});
Assert.That(list[“n2”],Is.equaletto(new[]{“ghi”}));
Assert.That(list[“n3”],相当于(new[]{“ghi”,“def”,“xyz”});
Assert.That(list[“n4”],Is.EquivalentTo(新字符串[]{}));
}
注意:这是上一个问题的后续问题:您想要左外联接、右外联接还是完全外联接?我想要列表中的每个
名称属性值及其关联的标记属性值,即使名称没有关联的标记s。我认为这是一个左或右的外部连接。在你前面的问题中,你可以在ToLookup中改为dictionary。