将字符串分组并添加到列表中 [英] Grouping Strings and Adding into a list

查看:95
本文介绍了将字符串分组并添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个复杂的情况,我非常困难。请告诉我你是否可以分享一些亮点。



我有一个



列表<类别和GT;哪个将具有以下属性

  public   class 类别
{
public string DisplayName {获得; set ; }
public string ValueCode { get ; set ; }
public string 计数{ get ; set ; }
}



这将具有类似值

< br /> 
Category1 / SubCategory1< br />
cat1 / sc1< br />
5< br />
< br />
Category1 / SubCategory2< br />
cat1 / sc2< br />
4< br />
< br />
类别2 /子类别1< br />
cat2 / sc1< br />
5< br />
< br />
类别2 /子类别2< br />
cat2 / sc2< br />
23



我创建了一个自定义类来填写值



 公开  JobCateogry 
{
public string DisplayName { get ; set ; }
public string ValueCode { get ; set ; }
public string 计数{ get ; set ; }
public 列表< JobCateogry>子类别{获取; set ; }
}



我必须在代码值中拆分字符串并将其分配给SubCategory。



喜欢我的最终退出职业类别将是



<代码> Category1 
Cat1
9
列表内
SubCategory1
sub1
5
SubCateogry2
sub2
4< / code>



我尝试拆分字符串,然后通过拆分然后通过分配将其分配给新类两步。但我确信我这样做是错误的,因为我分手的那一刻,我放松了计数。



< pre> ; var lstCategory = Categories 
.Where(i = > i.count > < span class =code-digit> 0 )
。选择(item = > item.valueCode.Split( /'
。选择(k = > (k))。ToList();





列表< JobCategories> jobcategories = lstCategory 
。选择(item = > item.Split(QueryStringConstants.CAT_SEPERATOR.ToCharArray()[ 0 ]))
.GroupBy(tokens = > 标记[ 0 ]。修剪(),标记= > 标记[ 1 ])
。选择(g = > new JobCategories(g.Key,g.DisplayName,g.ToList(), )。ToList();



你能帮忙吗?

解决方案

改变这个

  public 列表< jobcateogry>子类别{获取;  set ; } < /   jobcateogry  >  



to

  public  List<类别> 子类别{获取;  set ; } 


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

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

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 .

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



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?

解决方案

Change this

public List<jobcateogry> SubCategories { get; set; }</jobcateogry>


to

public List< Category > SubCategories { get; set; }


这篇关于将字符串分组并添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆