I could probably write this myself, but the specific way I’m trying to accomplish it is throwing me off. I’m trying to write a generic extension method similar to the others introduced in .NET 3.5 that will take a nested IEnumerable of IEnumerables (and so on) and flatten it into one IEnumerable. Anyone have any ideas?
Specifically, I’m having trouble with the syntax of the extension method itself so that I can work on a flattening algorithm.
Hmm… I’m not sure exactly what you want here, but here’s a ‘one level’ option:
If that’s not what you want, could you provide the signature of what you do want? If you don’t need a generic form, and you just want to do the kind of thing that LINQ to XML constructors do, that’s reasonably simple – although the recursive use of iterator blocks is relatively inefficient. Something like:
Note that that will treat a string as a sequence of chars, however – you may want to special-case strings to be individual elements instead of flattening them, depending on your use case.
Does that help?