My own implementation of Linq SelectMany extension method What do you think of my own implementation of the extension method SelectMany?
Motivating criticism is always welcome.

public static IEnumerable&lt;TResult&gt; MySelectMany&lt;T, TResult&gt;(this IEnumerable&lt;T&gt; source, Func&lt;T, IEnumerable&lt;TResult&gt;&gt; selector)
{
    var theList = new List&lt;TResult&gt;();

    foreach (T item in source)
    {
        foreach (TResult inneritem in selector(item))
        {
            theList.Add(inneritem);
        }
    }

    return theList as IEnumerable&lt;TResult&gt;;
}
The as cast in the return statement is entirely redundant, it doesn’t serve a purpose.

Furthermore, The problem with this implementation is that it’s not lazy. You should use a yield generator instead.

public static IEnumerable&lt;TResult&gt; MySelectMany&lt;T, TResult&gt;(this IEnumerable&lt;T&gt; source, Func&lt;T, IEnumerable&lt;TResult&gt;&gt; selector)
{
    foreach (T item in source)
        foreach (TResult inneritem in selector(item))
            yield return inneritem;
}


If C# already had a yield from statement, this would be even shorter since you wouldn’t need to iterate the inner items explicitly.