Linq and Missing UnionAll

While it’s a bit weird that Linq doesn’t come with UnionAll there are number of ways of implementing it. The first and the most obvious one is to write something like:

            public static IEnumerable<T> UnionAll<T>(IEnumerable<T> set1, IEnumerable<T> set2)
            {
                foreach (var s1 in set1)
                    yield return s1;

                foreach (var s2 in set2)
                    yield return s2;
            }

However according to Google this doesn’t seem to play nice with EF and Linq to SQL. The second way of achieving the same goal would be writing an extension method that uses the .Concat function of Linq

            public static IEnumerable<T> UnionAll<T>(IEnumerable<T> set1, IEnumerable<T> set2)
            {
                return set1.Concat(set2);
            }

This appears to achieve the desired effect. It’s worth noting that I haven’t tested the performance of the 2 implementation, but my guess is that in Linq to Object it would be almost identical

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: