views:

41

answers:

1

Hi folks,

I'm trying to filter a list of objects using linq. When i filter by a Contains(someSearchQuery) it's like it's being case sensitive .. meaning i miss some results.


I have an IList<Foo>'s which has a number of properties, but one is public string MyText { get; set; }

Now, i'm trying to return a IQueryable of Foo's where the MyText property contain the search query - but as a If this was a Sql statement, it would be either..

WHERE MyText LIKE '%searchQuery%' <-- Works but inefficent

or

WHERE CONTAINS(MyText, 'searchQuery') <-- using FTS.

I'm not sure how to do this, because when I do the following query it's like it's doing a case sensitive

var query = from q in myFooList.AsQueryable().Where(x => x.MyText.Contains(searchQuery));

Suggestions?

+5  A: 

You're asking about the string.Contains method, not about LINQ.

String.Contains does not support case-insensitive searching. Instead, you should call IndexOf, like this:

var query = from q in myFooList
     .Where(x => x.MyText.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0);

Note that since you're using LINQ-to-Objects, you (presumably) don't need AsQueryable.

SLaks
@SLaks - tha man! That works beautifully :) Out of interest, would this also work for Linq-To-Sql/Entities?
Pure.Krome
I don't know, but i would suspect not.
SLaks