Entity Framework - case insensitive Contains?

entity-framework

Question

I want a solution to this problem that does not involve ToUpper or ToLower, as I use in the code below;

var upper = term.ToUpper();
using (var db = this.DataContext)
{
    return db.Counties.Where(x => x.CountyName.ToUpper().Contains(upper)).ToList();
}

I am using entitly framework so the C# solution of using StringComparison.CurrentCultureIgnoreCase does not work. It does work for Equals, EndsWith and StartsWith, but not Contains.

1
35
7/8/2018 3:50:31 PM

Popular Answer

I use EF6 and Sql Server and Contains is mapped to LIKE '%@p0%' which is case insensitive in my case. So in my case:

db.Counties.Where(x => x.CountyName.Contains(term)).ToList();

works as needed. More info in Sjoerd answer.

8
5/23/2017 12:09:14 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow