Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a List that has strings something like this

"Category1/SubType1"
"Category1/SubType2"
"Category1/SubType3"  
"Category1/SubType4" 
"Category2/SubType1"
"Category2/SubType2"   
"Category2/SubType3" 
"Category2/SubType4"
"Category3"
"Category4"
"Category4/SubType1"
"Category4/SubType2 

i have a class that has two properties.

public class CategoriesClass
    {
        public CategoriesClass(string category, List<string> subCategory)
        {
            this.category = category;
            this.subCategory = subCategory.ToArray();
        }
        public string category { get; private set; }
        public string[] subCategory { get; private set; }
    }


I need to iterate through the list, and pick all the categories and then add subcategories to them.
Not all Categories have Sub Categories. So, the categories which don't have sub categories will have subcategory as null. And Some categories (like category 4 will have just the category name and then its sub categories)
I had sorted the list and I was able to achieve the traditional way by doing a for loop by taking the first string and comparing with the rest. I was wondering if there is any neater way to do this?
Posted
Updated 18-Dec-13 17:17pm
v3
Comments
BillWoodruff 19-Dec-13 7:29am    
Perhaps a Linq expert will come along and put a mind-blowingly terse solution here, but, in the meantime, unless we know how you coded the for-loop solution, we can't compare it with alternative approaches, or offer alternative ideas.

You could try something like this:

C#
internal class Cat{
    public string category;
    public string sub;
}

static void Main(string[] args)
{
    string[] categories = {
        "Category1/SubType1",
        "Category1/SubType2",
        "Category1/SubType3",
        "Category1/SubType4",
        "Category2/SubType1",
        "Category2/SubType2",
        "Category2/SubType3",
        "Category2/SubType4",
        "Category3",
        "Category4",
        "Category4/SubType1",
        "Category4/SubType2"};

    Dictionary<string, List<Cat>> categoryDict = categories.Select(c => c.Split('/'))
    .Select(a => new Cat()
    {
        category = a[0],
        sub = a.Length > 1 ? a[1] : string.Empty
    })
    .GroupBy(b => b.category)
    .ToDictionary(e => e.Key, e => e.ToList());

}
 
Share this answer
 
v2
I had a got a solution from different source.Had to modify it a bit.. But this worked . Posting it here for others..

C#
list.Select(item => item.Split('/'))
    .GroupBy(tokens => tokens[0], tokens => tokens[1])
    .Select(g => new TheClass
                     {
                         Category = g.Key,
                         SubCategory = g.ToList()
                     });
 
Share this answer
 

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