Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using string.compare in a linq query where clause

Tags:

linq

I am having a bit of trouble figuring out the exact syntax to use string.compare in the Where clause of a linq query. Below is what I have so far.

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

Is this even possible or am I barking up the wrong tree?

Rhonda

like image 280
Rhonda Avatar asked Mar 28 '12 18:03

Rhonda


1 Answers

If you want to check to see if Name contains the search text:

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

If you want to check for equality:

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

In your original query, you were checking to see if x.Name contains the result of string.Compare. I assume you weren't trying to do this, since string.Compare returns an integer. string.Compare is used primarily for determining sort order.

like image 90
Justin Rusbatch Avatar answered Sep 30 '22 17:09

Justin Rusbatch