我有List<string>
:
&#34; Item1&#34;,&#34; Item2&#34;,&#34; Item3&#34;,&#34; Item4&#34;
我希望新列表是:
&#34; Item1&#34;,&#34; Item1 / Item2&#34;,&#34; Item1 / Item2 / Item3&#34;,&#34; Item1 / Item2 / Item3 / Item4&#34;
我在如何以最简单的方式做到这一点上空白了。我基于以下内容对面包屑进行此操作:
var urlItems = Request.RawUrl.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
任何帮助将不胜感激。谢谢!
修改
感谢保罗,这是我的最终解决方案:
var urlItems = Request.RawUrl.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
var breadcrumbs = urlItems.Select((t, index) => string.Format("<li{0}><a href=\"/Gallery/{1}\">{2}</a></li>", (t == urlItems.Last() ? " active=\"active\"" : ""), string.Join("/", urlItems.Take(index + 1)), t)).ToList();
答案 0 :(得分:1)
List<string> myList = new List<string> { "Item1", "Item2", "Item3", "Item4" };
List<string> newList = new List<string>();
for (var index = 1; index <= myList.Count(); index++)
{
newList.Add(string.Join("/", myList.Take(index)));
}
答案 1 :(得分:1)
Linq:
List<string> list = new List<string>{"item1", "item2", "item3", "item4"};
string item = "";
var list2 = list.Select(i =>
{ return item = (item == "") ? i : string.Join(@"/", item, i); }).ToList();