Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

List<> split in 2

Posted on 2009-05-12
8
Medium Priority
?
356 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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