?
Solved

List<> split in 2

Posted on 2009-05-12
8
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

719 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