开发者

Using LINQ's SkipWhile over string properties

I have a list of strings and I want to skip all that end with the sequence _E so out of:

  • Apple
  • Apple_E
  • Orange
  • Banana

Only "Apple_E" should be omitted. This should be a si开发者_StackOverflow社区mple LINQ statement, correct?

foreach (var fruit in fruits.SkipWhile(x => x.EndsWith("_E")) 
{
       Console.WriteLine(fruit);
}

Will not omit Apple_E ... am I missing something obvious here? I have also used x.Trim().EndsWith("_E") to make sure there wasn't dirty data for some reason. If I do this, I get the list printed as shown above.


Are you sure you don't mean to use .Where()?

foreach (var fruit in fruits.Where(x => !x.EndsWith("_E")))
{
       Console.WriteLine(fruit);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜