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
  • 47,220
  • 23
  • 132
  • 141
h4mme7
  • 91
  • 10

2 Answers2

4

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

Jace
  • 757
  • 4
  • 17
2

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

food.StartsWith("s", StringComparison.OrdinalIgnoreCase);
Jace
  • 757
  • 4
  • 17
kemiller2002
  • 110,913
  • 27
  • 192
  • 245