baka panda
baka panda

Reputation: 11

c# Lambda Expression List of strings

I m confused about How can i Find a Specified Text In a A List using Lambda Expressions For Example i have a List

List<string> MyList = new List<string> {"TEXT","NOTEXT","test","notest"};

as you can see The List is Filtred By "ToUpper" and "ToLower" Properties i want for example to shearch on ToLower elemtes in this list using Lambda Expression

var SList = MyList.FindAll(item => item.ToLower);
foreach(var s in SList)
   {
      Console.WriteLine(s);
   }

Upvotes: 0

Views: 4976

Answers (2)

konkked
konkked

Reputation: 3231

Just check if the lowercase value matches against the current value, same for upper

var lower = MyList.Where(a=>a == a.ToLowerInvariant());
var upper = MyList.Where(a=>a == a.ToUpperInvariant());

If you want to use the culture specific version to check then just use the culture-specific methods

var lower = MyList.Where(a=>a == a.ToLower());
var upper = MyList.Where(a=>a == a.ToUpper());

Upvotes: 2

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

ToLower is not a property but a method and it does not make a test (i.e it does not return a bool) but returns a converted string.

This means, that since it is a method, you must place parentheses after it (.ToLower()).

In order to make a test you must compare the result with something. In this case with the original string, in order to see whether it is equal to the lower case string.

var SList = MyList.FindAll(item => item == item.ToLower());

The exact working of ToLower depends on the current UI-culture. Some languages have special rules for conversion to lower or upper case. If you prefer to have a culture independent behavior, use ToLowerInvariant or ToUpperInvariant instead.

Upvotes: 0

Related Questions