I have a class that looks like this:
public class Person
{
public string Name { get; set; }
public DateTime Birthdate { get; set; }
public List<string> PersonalEffects {get; set; }
}
I have a List<Person>, and I’d like to use the IEnumerable<T>.ToLookup() and use the name of each effect as the key, with the characters that have that effect as the values. Sample output:
Cutlass (the Effect)
Jack Sparrow (Person)
Will Turner
Pistol
Jack Sparrow
Hector Barbossa
Davy Jones
However, my LINQ-fu is failing me. I can do this through a Dictionary and a number of ForEach loops, but it doesn’t seem as clean (even though it does end up being the same thing).
How can I do this using ToLookup() in C#?
This would work:
What we ‘re doing here is essentially a join of persons and effects, creating person+effect pairs (where both specific persons and effects can appear in many pairs each), then grouping these pairs by effect and collecting the persons posessing each effect in an enumerable sequence.