我正在尝试让 GroupJoin 使用 LINQ 处理多个未知键。
我见过匿名类型的解决方案,但密钥总是预先定义的。就我而言,它们是用户定义的,所以我在编译时不知道这些信息。我尝试使用键值列表和键值数组,但它们从不匹配。
所以......这就像一个魅力:
Func<Component, string> getKeyValue = x => x.Attributes //from attributes
.Single(a => a.Name == _keyAttribute) //selects the key attribute
.Value; //gets attribute value
var leftJoin = source.GroupJoin(target, //join
getKeyValue, //on the same
getKeyValue, //condition
(src, corresp) => new
{
src,
corresp
})
.SelectMany(z => z.corresp.DefaultIfEmpty()
.Select(tgt => new { z.src, tgt })) //selects matching
.ToList(); //source and target
但这不是:
Func<Component, List<string>> getKeyValues = x => x.Attributes //from attributes
.Where(a => _keyAttributes.Contains(a.Name)) //selects key attributes
.OrderBy(a => a.Name) //order them by name
.Select(a => a.Value) //gets attributes' values
.ToList();
var leftJoin = source.GroupJoin(target, //join
getKeyValues, //on the same
getKeyValues, //condition
(src, corresp) => new
{
src,
corresp
})
.SelectMany(z => z.corresp.DefaultIfEmpty()
.Select(tgt => new { z.src, tgt })) //selects matching
.ToList(); //source and target
如果有帮助,这就是我正在研究的结构:
List<string> _keyAttributes;
List<Component> source;
List<Component> target;
[DataContract]
public class Component
{
[DataMember]
public List<Attribute> Attributes { get; set; }
public Component()
{
new List<Attribute>();
}
}
[DataContract]
public class Attribute
{
[DataMember]
public string Name { get; set;}
[DataMember]
public string Value { get; set;}
}
有没有办法使用 LINQ 库来解决这个问题,或者我需要自己的 GroupJoin 扩展方法来做到这一点?