Solved

List<> split in 2

Posted on 2009-05-12
8
348 Views
Last Modified: 2013-12-17
I have a Generic List (List<>).
I want to split it into 2 Lists. In the first one go items which indexnumber is an even number (0,2,4...)
and in the second items which indexnumber is an odd number (1,3,5...).
How is best to do that?
0
Comment
Question by:johnkainn
  • 4
  • 4
8 Comments
 
LVL 15

Expert Comment

by:Walter Ritzel
ID: 24366152
The first way I can think of is running thru the list and according to the index, adding in the odd or the even list.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24366183
List<YourObject> evenResults = mainList.FindAll(x=>x.IndexNumber % 2 == 0 || x.IndexNumber == 0);
List<YourObject> oddResults = mainList.Findall(x=>x.IndexNumber % 2 > 0);

maybe?
0
 
LVL 15

Expert Comment

by:Walter Ritzel
ID: 24366314
The only problem with this approach is that you depend upon using a property from the object inside the list to make the decision.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24366357
umm that's what the asker is basing the branching (separate lists) on. unless i am misunderstanding the question/purpose.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 22

Expert Comment

by:p_davis
ID: 24366369
and why would that be a problem? i am asking seriously b/c i am always willing to learn something new.
0
 
LVL 15

Accepted Solution

by:
Walter Ritzel earned 125 total points
ID: 24366400
This would be the standard way:
<eeSnippet>
    class SimpleObject
    {
        private int id;
        private String name;

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public SimpleObject(int p_id, String p_name)
        {
            this.id = p_id;
            this.name = p_name;

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Creating and Populating the list
            List<SimpleObject> mainList = new List<SimpleObject>();
            for (int i=0; i < 10; i++)
            {
                mainList.Add(new SimpleObject(i,"name " + i.ToString()));
            }

            //Approach not using LINQ
            List<SimpleObject> oddList = new List<SimpleObject>();
            List<SimpleObject> evenList = new List<SimpleObject>();
            int j = 0;
            foreach(SimpleObject obj in mainList)
            {
                if ((j % 2 == 0) || (j == 0))
                {
                    evenList.Add(obj);
                }
                else
                {
                    oddList.Add(obj);
                }
            }
        }
    }
</eeSnippet>
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 125 total points
ID: 24366411
that effectively does the exact same thing that i posted.
0
 
LVL 15

Expert Comment

by:Walter Ritzel
ID: 24366609
Sure, man. Let me explain: it's become a problem if, for example, your object does not have an IndexNumber or any number property... My solution does not consider the values inside the object, just the position inside the list.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now