8

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

2
  • String.Compare() returns an int. Commented Mar 28, 2012 at 18:56
  • What are you trying to do? You are using Contains and Compare... Commented Mar 28, 2012 at 18:58

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I believe you're looking for Equals if you're looking to match by equality:

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

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.