2

I have a LINQ statement as follows:

myList = MyList.Where(x => 
    x.Name.Contains(SearchValue) ||
    x.Address.Contains(SearchValue)).ToList();

My Database can have upper or lower cases for the Name field like 'VSTS' or 'vsts'
I want my LINQ statement to execute with any case.

In the above LINQ statement if i provide with lower case values and the database is having upper case, the searching fails.. which i don't want.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
tsvsdev
  • 121
  • 1
  • 1
  • 7
  • 1
    Is this actually in LINQ to Objects, or a database-oriented provider? – Jon Skeet Feb 12 '13 at 10:42
  • its Linq to Objects, I have extracted the values from the database prior executing My linq statement. – tsvsdev Feb 12 '13 at 10:45
  • Possible duplicate: http://stackoverflow.com/questions/444798/case-insensitive-containsstring/444813#444813 .. Have you even tried to use google, before asking the question? – mipe34 Feb 12 '13 at 11:01
  • yup i did.. but possible my keywords didn't matched to get the Link..i needed it on urgent basis.. – tsvsdev Feb 12 '13 at 11:09

5 Answers5

5

The best option would be using the ordinal case-insensitive comparison, however the Contains method does not support it, so your other option would be to use string.IndexOf.

It would be better to wrap this in an extension method, such as:

public static bool Contains(this string target, string value, StringComparison comparison)
{
    return target.IndexOf(value, comparison) >= 0;
}

So you could use:

myList.Where(x => x.Name.Contains(SearchValue, StringComparison.OrdinalIgnoreCase) ||
                  x.Address.Contains(SearchValue, StringComparison.OrdinalIgnoreCase));
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
1

Use the following:

myList = MyList.Where(x => 
    x.Name.ToUpper(CultureInfo.InvariantCulture).Contains(
        SearchValue.ToUpper(CultureInfo.InvariantCulture)) || 
    x.Address.ToUpper(CultureInfo.InvariantCulture).Contains(
        SearchValue.ToUpper(CultureInfo.InvariantCulture))).
    ToList();
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
0

Try this:

myList = MyList.Where(x => 
    x.Name.ToLower().Contains(SearchValue.ToLower()) || 
    x.Address.ToLower().Contains(SearchValue.ToLower())).ToList();

Check if this works for Linq2Entity, could be the tolower fails :p

JMan
  • 2,611
  • 3
  • 30
  • 51
0

you may try the ToLower():

myList = MyList.Where(x => x.Name.ToLower().Contains(SearchValue.ToLower()) 
    || x.Address.ToLower().Contains(SearchValue.ToLower())).ToList();
the_joric
  • 11,986
  • 6
  • 36
  • 57
0

If you need to use Contains() try this:

var svUP = SearchValue.ToUpper();
myList = MyList.Where(x => x.Name.ToUpper().Contains(svUP ) || x.Address.ToUpper().Contains(svUP )).ToList();

But if your fields contains full search value and only problem is casing then you should use Equals() with StingComparison instead:

myList = MyList.Where(x => x.Name.Equals(SearchValue, StringComparison.InvariantCultureIgnoreCase) || x.Address.Equals(SearchValue, StringComparison.InvariantCultureIgnoreCase)).ToList();
Schnapz
  • 1,208
  • 13
  • 10