0

I got following structure:

Class A has a member: List<ClassB>

Class B has a member: List<ClassC>

Who is it possible to get for one specific object of class A one specific object of List<B> and all of it's objects in List<ClassC>?

In a other way: How can I get the List<ClassC> for a specific ClassB?

Mike Corcoran
  • 14,072
  • 4
  • 37
  • 49
sampa
  • 535
  • 4
  • 27
  • Possible duplicate of [Difference Between Select and SelectMany](http://stackoverflow.com/questions/958949/difference-between-select-and-selectmany) – GSerg Apr 05 '17 at 17:50
  • Are you using EF and are those classes entities of your model? How do you know which `ClassB` object you want to get? – ocuenca Apr 05 '17 at 17:51

3 Answers3

1

Try:

var a_list = new List<A>();
var c_list = a_list.First(a => [your criteria here])
                   .b_list
                   .First(b => [your criteria here])
                   .c_list;
msitt
  • 1,237
  • 12
  • 27
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
0

You can try ( to avoid any exceptions)

 ClassA classAItem = yourClassAList.FirstOrDefault({your A condition})

    if(classAItem != null)
    {
      ClassB  classBItem = classAItem.yourClassBList.FirstOrDefault({your B condition});

      if(classBItem != null)
      {
        List<ClassC> classCItem = classBItem.yourClassCList);
      }
}
Ranjith V
  • 298
  • 1
  • 3
  • 16
0

Found the solution:

_db.ClassA.Include(b => b.ClassB).ThenInclude(c => c.ClassC);

Thanks for your answers!

sampa
  • 535
  • 4
  • 27