1

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!

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

3 Answers3

3

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
  • 6,594
  • 5
  • 44
  • 51
0

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
  • 7,411
  • 1
  • 23
  • 36
  • That returned a List> ???? 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 Mar 16 '09 at 01:00
  • 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 Mar 16 '09 at 01:14
0

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 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 Mar 16 '09 at 01:56