0

I'm looking for a generic class adapter that would enable me to iterate over a collection of collection (IEnumerable of IEnumerable) of objects through a single iterator.

In other words: How to fake a collection of collection by let it appears to the user as one single flat collection.

Example: For a structure like this:

public class Signal
{
  Point[] points;
}

public class Analysis
{
  List<Signal> Signals;
}

I would like to have a generic adapter that enable me to iterate over each point of an Analysis object.

The adapter would enable me to do:

  var analysisInnerCollectionOfPointAdapter = 
  new GenericCollectionOfCollectionAdapterIterator(myAnalysis);

        foreach(Point point in AnalysisInnerCollectionOfPointAdapter)
        {
          ...
        }

I do not want to create any duplicate collection. I just want to make abstraction of the inner step (collection of collection) from my iteration.

I just start try to program it but I wonder if there is not something that already exists that would probably be better than my potential future creation. Perhaps in an existing library like Linq or MoreLinq? Perhaps you already done it and have something nice?

Edit (an hour after): I just found: Difference Between Select and SelectMany which give a good example of what I want. Thanks to AlexeiLevenkov.

Community
  • 1
  • 1
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • 1
    You mean "Whould not it be nice if there `Enumerable.SelectMany`? – Alexei Levenkov Jul 12 '15 at 20:45
  • @AlexeiLevenkov That still creates a copy of the data – DavidG Jul 12 '15 at 20:57
  • @DavidG SelectMany does not create any copies - it is similar to most other LINQ operations like Select/Where... Not sure what code you have in mind saying "copy of the data" (I don't believe you can avoid copying value types, but it does no look like OP needs to avoid that)... `analysis.Signal.SelectMAny(x=>x)` only copies `Point` assuming it is value types... – Alexei Levenkov Jul 12 '15 at 21:04
  • :-/ ... Doh!!! Sounds like you are right. I'm giving a try right now. I don't feel a bit stupid, just a lots! I come back in an hour or 2 (my code is far from compiling). Thanks a lot... Perhaps you could write an answer or should I just delete my question if you are right??? I think there is no copy because it probably uses yield behind the scene. – Eric Ouellet Jul 12 '15 at 21:06
  • @AlexeiLevenkov, You were right. Can you right an answer? – Eric Ouellet Jul 14 '15 at 18:48

1 Answers1

0

After few months... AlexeiLevenko did not post his answer:

SelectMany does not create any copies - it is similar to most other LINQ operations like Select/Where... Not sure what code you have in mind saying "copy of the data" (I don't believe you can avoid copying value types, but it does no look like OP needs to avoid that)...

analysis.Signal.SelectMAny(x=>x) only copies Point assuming it is value types...

Community
  • 1
  • 1
Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • If your answer is another's work, you should make the answer community wiki. – durron597 Sep 02 '15 at 17:26
  • I think the link you refer to is : http://stackoverflow.com/questions/19601143/whats-the-best-way-to-apply-a-join-method-generically-similar-to-how-string-j – Eric Ouellet Sep 08 '15 at 14:56