Solved

List<> split in 2

Posted on 2009-05-12
8
349 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 16

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 16

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 
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 16

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 16

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
VB: Convert 2 dates to specific format 24 48
localhost: This site can’t be reached  localhost refused to connect. 2 49
SQL Help 27 43
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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