linqentityset

Struggling with .ToList() returing an EntitySet<...>, not an IList<..>


I'm trying to retrieve a list of Id's from a collection that is a few levels deep in an object heirachy. When i try to do a ToList(), I keep getting an EntityList<> retrieved instead .. which means it's not allowing me to retrieve an instance's BarId property because the EntitySet is a Enumerable, not a single instance object.

Foo.Child1 (1 to 1)
Child1.Children2 (0 to many of type Bar)
Bar.BarId int;

IList<Foo> fooList = (from blah blah blah).ToList();

var children2List = (from x in fooList
select x.Child1.Children2).ToList();

It keeps returning children2List as an EntitySet<Bar>, not an IList<Bar>. As such, i'm struggling to retrieve the list of BarId's from children2List.

please help!


Solution

  • Your can use:

    var children2List = fooList.SelectMany( x => x.Child1.Children2 ).ToList();
    

    This will allow you to do something like:

    children2List.ForEach( b => b.BarId.Print() );