Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I combine multiple IEnumerable list together

I have a class (ClassA) that has a IEnumerable property. I then has another class (ClassB) that has the same property. They are sharing an interface (InterfaceA). The ClassB is basically a container class for multiple ClassA's. How to I implement the property for ClassB.

interface InterfaceA
{
    IEnumerable<int> MyInts
    {
        get;
    }
}

class ClassA : InterfaceA
{
    public IEnumerable<int> MyInts
    {
        get;
        private set;
    }
}

class ClassB : InterfaceA
{
    ClassA[] classAs = new ClassA[10];
    public IEnumerable<int> MyInts
    {
        get
        {
            //What goes here ?
            classAs.SelectMany(classA => classA.MyInts);
        }

    }
}

I tried using a LINQ select statement but that doesn't work.

Edit: Looks like I didn't look hard enough. The answer is here in this question. How do I Aggregate multiple IEnumerables of T

Edit 2: Include the example code that worked for me, incase anyone else needs it.

like image 419
Robin Robinson Avatar asked Aug 03 '09 15:08

Robin Robinson


2 Answers

Just adding my answer, so this doesn't go unanswered.

classAs.SelectMany(classA => classA.MyInts);

Found this from this question.

like image 90
Robin Robinson Avatar answered Nov 04 '22 07:11

Robin Robinson


        public IEnumerable<int> MyInts
        {
            get
            {
                foreach (ClassA c in classAs)
                {
                    foreach (int i in c.MyInts)
                    {
                        yield return i;
                    }
                }
            }

        }
like image 29
CD.. Avatar answered Nov 04 '22 07:11

CD..