0

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);
   }

2 Answers 2

2

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());
Sign up to request clarification or add additional context in comments.

1 Comment

@bakapanda No problem, please mark as the answer if this has resolved your issue
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.