C#String拆分路径以获取每个子路径

时间:2014-07-03 16:45:39

标签: c# string linq

我有这个数据集:

  • BW \ Website Implementation \ Phase1 \ Backlog
  • BW \ Website Implementation \ Phase1 \ Iteration 0

    List<string> IterationPaths = new List<string>();
    foreach (WorkItem item in collection)
    {
      List<string> itpath = item.IterationPath.Split('\\').ToList<string>();
      itpath = itpath.Except(IterationPaths).ToList();
      foreach (string path in itpath)
      {
        IterationPaths.Add(path);
      }
    }
    

但目前这给了我:

  • BW
  • 网站实施
  • 阶段1
  • 积压
  • 迭代0

我需要它:

  • BW
  • BW \网站实施
  • BW \ Website Implementation \ Phase1
  • BW \ Website Implementation \ Phase1 \ Backlog
  • BW \ Website Implementation \ Phase1 \ Iteration 0

我必须做些什么调整才能发挥作用?

1 个答案:

答案 0 :(得分:4)

var itpath = item.IterationPath.Split('\\');

int i = 1;
var result = itpath.Select(x => string.Join("\\", itpath.Take(i++))).ToList();