Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a List Contain 'N' values like

Pre-aa,Post-aa,Normal-aa,Post-aa,Normal-aa,Normal-aa,Post-aa,Pre-aa,Post-aa,Pre-aa,Post-aa,Post-aa,Normal-aa,Normal-aa....etc

How to create a seperate new list for each Pre-aa, Post-aa and Normal-aa from the parent List.

Please Help

Thanks in Advance
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jan-15 3:09am    
What have you tried so far?
—SA
Zoltán Zörgő 29-Jan-15 16:40pm    
"aa" is just "aa" or they represent some pattern?

1 solution

Example using Linq.


C#
using System;
using System.Collections.Generic;
using System.Linq;


C#
private static List<List<string>> Split(IList<string> source)
        {
            if (source == null || source.Count == 0)
            {
                return null;
            }            
            else
            {
                return source
                    .Select(x => x)
                    .GroupBy(x => x)
                    .Select(x => x.ToList())
                    .ToList();
            }
        }
static void Main(string[] args)
        {
            List<string> array = new List<string>();
            array.Add("Pre-aa");
            array.Add("Post-aa");
            array.Add("Normal-aa");
            array.Add("Post-aa");
            array.Add("Normal-aa");
            array.Add("Normal-aa");
            array.Add("Post-aa");
            array.Add("Pre-aa");
            array.Add("Post-aa");
            array.Add("Pre-aa");
            array.Add("Post-aa");
            array.Add("Post-aa");
            array.Add("Normal-aa");
            array.Add("Normal-aa");
            var result = Split(array);
            // Results in three Lists


}
 
Share this answer
 
v4
Comments
CHill60 29-Jan-15 3:19am    
That doesn't compile - what is a "list"?
sashje- 29-Jan-15 3:28am    
Sorry .. wrongly pasted code.. I'll improve the answer
sashje- 29-Jan-15 3:24am    
I added my using statements.. A List is a generics type from System.Colletion.Generic.List<t>, where T is string in my example..
You said you had a list in your question.. What exactly is you dataobject which you want to group and split?
CHill60 29-Jan-15 3:31am    
:-) I know what a "List" is ... I was pointing out your mistake of "List<list><string>>" that you had originally.
Oh - and the question wasn't mine ;-)
CHill60 29-Jan-15 3:33am    
Not sure why this was downvoted ... the Split function does exactly what the OP wanted. 5'd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900