Given this object structure
class A
{
public List<B> Bs { get; set; }
}
class B
{
public List<C> Cs { get; set; }
}
class C
{
public int D { get; set; }
}
And this initialization
A a = new A();
a.Bs = new List<B>();
a.Bs.Add(new B() { Cs = new List<C>() { new C() { D = 4 }, new C() { D = 5 } } });
a.Bs.Add(new B() { Cs = new List<C>() { new C() { D = 2 }, new C() { D = 3 } } });
You can find all instances of C where D equals 4 like this
var query = from b in a.Bs
from c in b.Cs
where c.D == 4
select c;
The result type would be IEnumerable<C>
. If you wanted or expected a single C, you could modify the query slightly.
C firstC = (from b in a.Bs
from c in b.Cs
where c.D == 4
select c).FirstOrDefault();
Same queries in extension/lambda form
var allCs = a.Bs.SelectMany(b => b.Cs).Where(c => c.D == 4);
C firstC = a.Bs.SelectMany(b => b.Cs).FirstOrDefault(c => c.D == 4);