I'm trying to get a GroupJoin to work with multiple unknown keys using LINQ.
I've seen solutions with anonymous types, but the keys were always pre-defined. In my case, they're user-defined, so I wouldn't know that information at compile time. I tried to use a list of key values and an array of key values, but they never match.
So... this works like a charm:
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
but this doesn't:
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
If it helps, this is the structure I'm working on:
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;}
}
Is there a way to solve this using LINQ library or I'd need my own GroupJoin extension method to do that?