Take the 2-minute tour ×
public class Buddy
		public string[] Sayings;

Buddy one = new Buddy();
		one.Sayings = new[] {"cool", "wicked", "awesome"};

		Buddy two = new Buddy();
		two.Sayings = new[] {"bad", "lame", "boring"};

		Buddy[] buddies = new[] {one, two};

		IEnumerable<string[]> something =
			from b in buddies
			select b.Sayings;

So basically I would like to get a single array or list that contains {"cool, wicked, awesome, "bad", "lame", "boring"} the sayings for each Buddy using linq.

I tried everything I could think of and am starting to doubt it can be done with just a single linq expression. I could go through each buddy in buddies and do an addrange on the Sayings into a list but I figured since I am learning linq I would try it this way. Is it possible and if so how? This could also apply if I wanted to get some other objects inside of a Buddy in this case its just the list of strings.

share|improve this question
Thanks Jakers and Jon for the explanation. I was almost there damn it. Cheers –  ElvisLives Jun 17 '09 at 20:40
Should one.Sayings = new[] {"cool, wicked, awesome"}; be: one.Sayings = new[] { "cool", "wicked", "awesome" }; –  RichardOD Jun 17 '09 at 20:41
Thanks fixed the typo. –  ElvisLives Jun 17 '09 at 20:49

4 Answers 4

up vote 4 down vote accepted

How about this:

var result2 = buddies.SelectMany(b => b.Sayings);
share|improve this answer
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);
share|improve this answer
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( b => b.Sayings ).ToArray();
share|improve this answer

    	string[] a = new string[] {"a", "b", "c", "d"};
    		string[] b = new string[] {"1", "2", "3"};

    	var unioned = a.Union(b);
    	foreach(string s in unioned)
share|improve this answer

Your Answer


