0

I was working on something that required me to catch the first char of a string. and return if it starts with S or s.

The pseudocode I considered was:

var foodsWithS = Foods.Where(food => food.Name[0] == 'S' || food.Name[0] == "s").Select(i => i.Name);

Unfortunately, this fails. What would be the best way to do this? Currently I have two lambdas to check for each case of the letter.

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
h4mme7
  • 91
  • 10

2 Answers2

4

foods.Where(x => x.StartsWith("S", StringComparison.OrdinalIgnoreCase));

Jace
  • 777
  • 1
  • 5
  • 18
2

If you are using a collection of string objects, You can also use StartsWith;

food.StartsWith("s", StringComparison.OrdinalIgnoreCase);
Jace
  • 777
  • 1
  • 5
  • 18
kemiller2002
  • 113,795
  • 27
  • 197
  • 251