cagin
cagin

Reputation: 5930

Entity Framework contains clause doesn't find

I use contains clause for searching in db table. But if the data has upper case letter and if I search with lower cases, it doesn't find what I search. But it can find when I search with upper case letter. It is the same with lower case searches.

Here is my code:

 using (var context = new eTicaretEntity())
  {
    return context.GetActiveProducts().Where(p => p.Name.Contains(name)).ToList();
  }

And you can see what I mean is these pics.

It can find with upper case letter.

It can find

It couldn't find data with lower case letter. enter image description here

Upvotes: 1

Views: 815

Answers (2)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14302

Try this instead :

return context.GetActiveProducts().Where(p => 
          p.Name.IndexOf(name,StringComparison.OrdinalIgnoreCase) >= 0).ToList();

This will fix your issue.

Upvotes: 1

jrummell
jrummell

Reputation: 43087

If your database collation is case sensitive, then you'll have to convert both sides of the comparison to upper (or lower) case.

using (var context = new eTicaretEntity())
{
    return context.GetActiveProducts()
                  .Where(p => p.Name.ToUpper().Contains(name.ToUpper()))
                  .ToList();
}

Upvotes: 3

Related Questions