给出这样一个列表:
["singing", "office", "1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"]
选择列表中包含字符“>”的第一个元素,最简洁的LINQ查询是什么和之后的所有元素。对于上面的例子,我想要这个结果:
["1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"]
我显然可以循环并手动完成,但感觉就像那里有一个优雅的单线。这是手动尝试:
var found = false;
var filtered = new List<string>();
foreach (var itm in list){
if (itm.Contains(">")) found = true;
if (found) filtered.Add(itm);
}
Console.WriteLine(filtered);
答案 0 :(得分:6)
我认为您正在寻求SkipWhile
扩展方法:
var ar = new string []{ "singing", "office", "1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"};
var r= ar.SkipWhile(s => !s.Contains(">"));