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);
}
精彩评论