Using string.compare in a linq request, where is the condition

I'm having trouble calculating the exact syntax for using string.compare in the Where clause of the linq query. Below I am still.

filteredApplications = AllApplications.Where(x => x.Name.Contains(string.Compare(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase))).ToList(); 

Is this possible, or am I barking the wrong tree?

Rhonda

+8
linq
source share
2 answers

If you want to check if Name contains search text:

 AllApplications.Where(x => x.Name.ToUpperInvariant().Contains(txtSearch.Text.ToUpperInvariant()))).ToList(); 

If you want to check equality:

 AllApplications.Where(x => string.Equals(x.Name, txtSearch.Text, StringComparison.OrdinalIgnoreCase)).ToList(); 

In the original query, you checked if x.Name result of string.Compare . I assume you did not try to do this, since string.Compare returns an integer . string.Compare is mainly used to determine the sort order.

+11
source share

I believe that you are looking for Equals if you want to match according to the principle of equality:

 filteredApplications = AllApplications.Where(x => x.Name.Equals(txtSearch.Text, StringComparison.OrdinalIgnoreCase)).ToList(); 
0
source share

All Articles