I have a question using these same examples - this question is focused on a different issue. Given the following classes:
[XmlRoot]
public class Family {
[XmlElement]
public List<Person> Person;
}
public class Person {
[XmlAttribute("member")]
public MemberType Member { get; set; }
[XmlAttribute("id")]
public int Id { get; set; }
[XmlElement]
public string Surname { get; set; }
[XmlElement]
public string Forename { get; set; }
[XmlElement("Person")]
public List<Person> People;
}
public enum MemberType {
Father,
Mother,
Son,
Daughter
}
If Family
has a method defined as such:
public IEnumerable<Person> Find (Func<Person, bool> predicate) {
// how do I get SelectMany to flatten the list?
foreach (var p in family.Person.SelectMany(p => p)) {
if(predicate(p)) {
yield return p;
}
}
}
I need to be able to execute the predicate over a flattened list of Person
. In the example above SelectMany
is not flattening the list as I had hoped. The above actually won't compile because the inferred type cannot be determined.
How can I get the Family.Person collection to become one flattened list of Person?