tags:

views:

887

answers:

3

Hi folks,

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!

A: 

In your query, you turn the whole result into a list, not the individual Children2 sets. Try

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

This will turn each Children2 into a List.

Denis Troller
That returned a List<List<Bar>> ???? and i'm guessing you're missing a () in the first ToList ... I still can't get the BarId from the result list....
Pure.Krome
Sorry, I thought that was what you wanted.So you want to flatten the whole thing down to 1 list ?And Yes, I'm missing a () because I was typing directly in SO :)
Denis Troller
+2  A: 

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() );
bstoney
A: 

EntitySet<T> implements IList<T>, so you already are returning IList<Bar>.

John Saunders
that's what i thought, but it wasn't allowing me to interigate each item. it was also saying i had a collection, not an item.
Pure.Krome