I have a special issue I want to solve by using LINQ but my solution does not work yet. I condensed the structure to the minimum and hope it is easy to understand. The classes are as follows:
public class A
{
public List<B> ListOfBs { get; set; }
}
public class B
{
public List<C> ListOfCs { get; set; }
}
public class C
{
public int ID { get; set; }
public string SomeText { get; set; }
}
In my code I want to filter for specific C's and set them into a new List as property of a new instance of B (or even A). So I tried the following Linq query within a function:
public B FilterFunction(A a, string someText)
{
B FilteredB = new B();
FilteredB.ListOfCs = (from b in a.ListOfBs
from c in b.ListOfCs
where c.SomeText == someText
select c).ToList();
return FilteredB;
}
however, the returned list is empty, so I guess my LINQ query is not correct. Any suggestions? Thank you in advance Martin
Athat contains aList<B>and eachBcontains aList<C>, and you want to create and return a single newBthat contains aList<C>where eachCmatches a specific condition? If so then that is a job forSelectMany. I'll put together an answer to that effect.