0

Okay so I have a POCO class that may contain another POCO class as an array. In some instances when I get the data I want to CREATE a list of the lists but not as a level down but all on the same level. I think I am missing something very simple so I thought I would ask here. I keep trying different syntax for Lambdas but the data is there, I can just never make it appear near the top. I would like the solution to be in lambdas if possible instead of doing the old school foreach. I was not sure if you can do this inline at all or if you have to declare a collection first and then add to it. Where I am at:

class Program
    {
        public class lowerlevel
        {
            public string ChildName;
        }

        public class upperlevel
        {
            public string ItemName;

            public lowerlevel[] ChildNames;
        }

        static void Main(string[] args)
        {
            // Create a list of a POCO object that has lists in it as well.
            List<upperlevel> items = new List<upperlevel>
                {
                    // declaration of top level item
                    new upperlevel
                    {
                        ItemName = "FirstItem",
                        // declaration of children
                        ChildNames = new lowerlevel[] 
                            {new lowerlevel {ChildName = "Part1"}, new lowerlevel {ChildName = "Part2"}},

                    },
                    // declaration of top level item
                    new upperlevel
                    {
                        ItemName = "SecondItem",
                        // declaration of children
                        ChildNames = new lowerlevel[] { new lowerlevel { ChildName = "Part3" } }
                    }
                };


            var stuff = items.Select(l1 => l1.ChildNames.ToList().Select(l2 => 
                new lowerlevel
                {
                    ChildName = l2.ChildName
                }))
                .ToList();

            // Arghh!  I just want to make a new list with lambdas that is NOT nested a level down!  This is NOT what I want but it is valid.
            stuff.ForEach(n => n.ToList().ForEach(n2 => n2.ChildName));

            // I want this but it does not work as I am not doing the expression right 
            // stuff.Foreach(n => n.ChildName);

        }

    }
2
  • 1
    I think SelectMany() is what you are looking for. Commented May 10, 2013 at 18:32
  • That appears to be it if I place it right after 'items' in my example. Thanks! Commented May 10, 2013 at 19:30

1 Answer 1

1

Try using a SelectMany() Rather than .Select()

var stuff = items.SelectMany...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.