Solved

# List<> split in 2

Posted on 2009-05-12
347 Views
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
Question by:johnkainn
• 4
• 4

LVL 15

Expert Comment

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

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

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

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

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

Walter Ritzel earned 125 total points
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++)
{
}

//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))
{
}
else
{
}
}
}
}
</eeSnippet>
0

LVL 22

Assisted Solution

p_davis earned 125 total points
that effectively does the exact same thing that i posted.
0

LVL 15

Expert Comment

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

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…