Solved

List<> split in 2

Posted on 2009-05-12
8
347 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
umm that's what the asker is basing the branching (separate lists) on. unless i am misunderstanding the question/purpose.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:p_davis
Comment Utility
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
Comment Utility
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
Comment Utility
that effectively does the exact same thing that i posted.
0
 
LVL 15

Expert Comment

by:Walter Ritzel
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now