Is there a linq-y way to union collection properties of a collection of objects?

Here's the obligatory method form return parents . SelectMany(p => p. Children) And for two levels: return oldies .

SelectMany(grand => grand. Children) . SelectMany(parent => parent.

Children).

Here's the obligatory method form. Return parents . SelectMany(p => p.

Children); And for two levels: return oldies . SelectMany(grand => grand. Children) .

SelectMany(parent => parent. Children).

This will work: public IEnumerable GetAllChildren(IEnumerable parents) { return from parent in parents from child in parent. Children select child; } and then this: public IEnumerable GetAllChildren(IEnumerable nanas) { return from papa in nanas from parent in papa. Children from child in parent.

Children select child; } Note, in this example I'm not actually returning a list, I'm returning an IEnumerable data source that until you start to foreach over it, or similar, won't actually do any processing. If you need to return a list, modify each return statement as follows: return (from ..... ... select child).ToList().

The return value has to be an IEnumerable, and the easiest way (at least as far as I know) to do that is return a collection that implements IEnumerable. Thanks for the answer! – Will?

Oct 15 '08 at 15:45 You can return a collection, or use yield return, look up the yield keyword on msdn if you're not familiar with it. – Lasse V. Karlsen?

Oct 15 '08 at 15:46 Good point on the yield... I usually don't do that in these types of cases due to a step along the way failing, in which case I just return the empty collection. Its pretty obvious what I'm doing, whereas a yield sometimes isn't clear what happens in these cases for people who come behind me... – Will? Oct 15 '08 at 17:55.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions