Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Complex Situation now and i am terribly stuck. Kindly Let me know if you can share some light to it.

I have a

List<categories> Which will have the Following properties
C#
public class Categories 
    {
        public string DisplayName { get; set; }
        public string ValueCode { get; set; }
        public string Count { get; set; }           
    }

This will have Values like
<br />
Category1/SubCategory1<br />
cat1/sc1<br />
5<br />
<br />
Category1/SubCategory2<br />
cat1/sc2<br />
4<br />
<br />
Category 2/Subcategory1<br />
cat2/sc1<br />
5<br />
<br />
Category 2/Subcategory2<br />
cat2/sc2<br />
23

I created a Custom Class to fill in the values

C#
public class JobCateogry
   {
       public string DisplayName { get; set; }
       public string ValueCode { get; set; }
       public string Count { get; set; }
       public List<JobCateogry> SubCategories { get; set; }
   }

I have to Split the String in the Code Value and assign it to the SubCategory.

Like My Final out of jobCategory would be

<code>Category1
Cat1
9
 inside the list 
    SubCategory1
    sub1
    5
    SubCateogry2
    sub2
    4</code>

I tried to Split the string and assign it to the new class in two step first by splitting and then by assiging. But i am sure i am doing it the wrong way, because the moment i split, i loose the count .

C#
<pre>var lstCategory =  Categories 
                        .Where(i => i.count > 0)
                        .Select(item => item.valueCode.Split('/')                                        
                        .Select(k =>(k)).ToList();


C#
List<JobCategories> jobcategories = lstCategory
    .Select(item => item.Split(QueryStringConstants.CAT_SEPERATOR.ToCharArray()[0]))
    .GroupBy(tokens => tokens[0].Trim(), tokens => tokens[1])
    .Select(g => new JobCategories(g.Key,  g.DisplayName,g.ToList(),)).ToList();

Can you please help?
Posted
Comments
BillWoodruff 14-Jan-14 1:13am    
You need to edit your post, and make your description much simpler. Make it clear exactly what is not working ... or has errors ... step-by-step. Exactly what is the relationship between the class 'Category and the class 'JobCategory ?

1 solution

Change this
C#
public List<jobcateogry> SubCategories { get; set; }</jobcateogry>

to
C#
public List< Category > SubCategories { get; set; }
 
Share this answer
 
Comments
Member 10475792 14-Jan-14 1:09am    
Could not understand as to how would that solve the problem. Can You explain? ?

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