Это будет работать: https://dotnetfiddle.net/ckwCs1
public static void DisplayFemaleChildrenAndParents(List<Parent> Parents)
{
List<Parent> parentsAndFemaleChildren = Parents.Where(p => p.Children.Any((c => c.Gender == "Female"))).ToList();
Console.WriteLine("\nShould Display Parents and their Female children details ONLY\n");
foreach(Parent p in parentsAndFemaleChildren)
{
Console.WriteLine("***********");
foreach(Child c in p.Children.Where(c=>c.Gender == "Female"))
{
Console.WriteLine(p.ParentName + " - " + c.ChildName + " - " + c.Gender);
}
Console.WriteLine("***********");
}
}
***********
Parent 1 - Parent 1 Child 1 - Male
Parent 1 - Parent 1 Child 2 - Female
***********
***********
Parent 2 - Parent 2 Child 1 - Female
Parent 2 - Parent 2 Child 2 - Male
***********
***********
Parent 3 - Parent 3 Child 1 - Male
***********
Should Display Parents and their Female children details ONLY
***********
Parent 1 - Parent 1 Child 2 - Female
***********
***********
Parent 2 - Parent 2 Child 1 - Female
***********
Сначала вы выбираете родителей, у которых вообще есть дети женского пола, а затем отфильтровываете всех детей мужского пола, поэтому отображаются только женщины.
EDIT:
Другой вариант:
https://dotnetfiddle.net/sFObju
public static void DisplayFemaleChildrenAndParents(List<Parent> Parents)
{
List<Parent> parentsAndFemaleChildren = Parents.Where(p => p.Children.Any((c => c.Gender == "Female"))).ToList();
Console.WriteLine("\nShould Display Parents and their Female children details ONLY\n");
foreach(Parent p in parentsAndFemaleChildren)
{
Console.WriteLine("***********");
foreach(Child c in p.Children.Where(c=>c.Gender == "Female"))
{
Console.WriteLine(p.ParentName + " - " + c.ChildName + " - " + c.Gender);
}
Console.WriteLine("***********");
}
Console.WriteLine("\n\n *********** v2 *********** \n\n");
// alternative
var parentsWithFemales = Parents.Select(p => new {p, FemaleChildren = p.Children.Where(a => a.Gender == "Female")})
// Filter out parents without female children
.Where(x => x.FemaleChildren.Any());
foreach(var pc in parentsWithFemales)
{
Console.WriteLine(pc.p.ParentName + "\tfemale children:");
foreach(var c in pc.FemaleChildren)
Console.WriteLine("\t"+c.ChildName + ", Gender: " + c.Gender);
}
}
***********
Parent 1 - Parent 1 Child 1 - Male
Parent 1 - Parent 1 Child 2 - Female
***********
***********
Parent 2 - Parent 2 Child 1 - Female
Parent 2 - Parent 2 Child 2 - Male
***********
***********
Parent 3 - Parent 3 Child 1 - Male
***********
Should Display Parents and their Female children details ONLY
***********
Parent 1 - Parent 1 Child 2 - Female
***********
***********
Parent 2 - Parent 2 Child 1 - Female
***********
*********** v2 ***********
Parent 1 female children:
Parent 1 Child 2, Gender: Female
Parent 2 female children:
Parent 2 Child 1, Gender: Female
Edit2 :
https://dotnetfiddle.net/N8K0AF
var parentsWithFemales = Parents.Select(p => new Parent(){ParentId = p.ParentId, ParentName = p.ParentName, Children = p.Children.Where(a => a.Gender == "Female").ToList()})
// Filter out parents without female children
.Where(x => x.Children.Any());
foreach(var pc in parentsWithFemales)
{
Console.WriteLine(pc.ParentName + "\tfemale children:");
foreach(var c in pc.Children)
Console.WriteLine("\t"+c.ChildName + ", Gender: " + c.Gender);
}
Вывод такой же, как указано выше:
Parent 1 female children:
Parent 1 Child 2, Gender: Female
Parent 2 female children:
Parent 2 Child 1, Gender: Female