Use a linq expression to get a single array or list of strings from inside a collection of objects without using foreach and addrange?

How about this: var result2 = buddies. SelectMany(b => b. Sayings).

That works but what about if I have n number of buddies meaning I don't know the size of the collection. I forgot to mention the that the example only includes 2 buddy objects but in reality it could be 1000. I don't want to have to union each one with the next since the size is unknown.

– ElvisLives Jun 17 '09 at 20:31 Sorry, there you go. – Jake Pearson Jun 17 '09 at 20:32 I was trying to work out why this wasn't acceptable and now it is the selected answer! – RichardOD Jun 17 '09 at 20:43.

Jakers is right - this is exactly what SelectMany does, in its simplest form. Alternative overloads allow you to get different results, e.g. Including the "source" item in the projection as well. Note that the query expression syntax for this is to have more than one from clause - each clause after the first one adds another call to SelectMany.

Jakers code is similar to: var result2 = from buddy in buddies from saying in buddy. Sayings select saying; except the dot-notation version is more efficient - it only projects once. The above code compiles to: var result2 = buddies.

SelectMany(buddy => buddy. Sayings, (buddy, saying) => saying).

If the Buddy array wasn't in the question code I would of done this (as it wasn't initially clear what was required): var result2 = one.Sayings. Concat(two. Sayings); – RichardOD Jun 17 '09 at 20:48.

This should work ... var buds = new { new Buddy { Sayings = new {"1","2","3"} }, new Buddy { Sayings = new {"4","5","6"} }, new Buddy { Sayings = new {"7","8","9"} } }; var res = buds. SelectMany( be => b. Sayings ).ToArray().

String a = new string {"a", "b", "c", "d"}; string be = new string {"1", "2", "3"}; var unioned = a. Union(b); foreach(string s in unioned) Console. WriteLine(s).

Alternative overloads allow you to get different results, e.g. including the "source" item in the projection as well. Note that the query expression syntax for this is to have more than one from clause - each clause after the first one adds another call to SelectMany. Except the dot-notation version is more efficient - it only projects once.

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