System.Linq.Dynamic.Core - Case insensitive Contains() How to use the Contains() method in a case-insensitive way using Entity Framework Core and System.Linq.Dynamic.Core

BuildWebHost - Unable to create an object of type ApplicationDbContext error in EF Core 2.x - How to Fix

A few days ago we published a small post explaining how to perform a case insensitive Contains query using Entity Framework Core, since the Contains() method changed its behaviour from being case-insensitive (Entity Framework and Entity Framework Core 1.0) to case-sensitive (EF Core 1.1 and onwards). In this post we'll expant that answer to see how the fix that we've suggested there can be implemented when we build our LINQ-based queries using System.Linq.Dynamic.

System.Linq.Dynamic: how it works

For those who don't know what System.Linq.Dynamic actually is, let's just say that we're talking about one of the best NuGet packages you can get while working with Entity Framework, which also has an awesome .NETStandard & .NET Core port called System.Linq.Dynamic.Core that can be used with Entity Framework Core: in a nutshell, it's a library that adds dynamic language functionalities to LINQ using a powerful set of extensions that could be used to turn something like this:

into this:

or (more conveniently) into this:

or (even more conveniently) into this:

As we can easily guess, @0 and @1 are placeholders for variables, just like {0} and {1} are for the string.Format method.

For additional info regarding System.Linq.Dynamic.Core and a sample use case scenario, take a look to our ASP.NET C# – set Column names programmatically with Dynamic Linq post.

The issue

Now that we know what we're talking about, let's come to the main topic of this post. System.Linq.Dynamic is awesome, but its Core counterpart - since it relies upon Entity Framework Core - suffers from the same case sensitivity issue that affects its "big uncle".

More specifically, if we do this:

The Contains() method will be processed in a case-sensitive way: this means that if the DB column represented by [field] contains the word "Monkey" and strValue has a value of "monk", the method will return False.

The fix

Luckily enough ,there at least two ways to overcome such issue and being able to use the Contains() method in a case-insensitive way:

Method #1: ugly, but works

Here's a viable fix:

Ah, the good old "double lowecase" approach: not too bad, isn't it? However, we can do better.

Method #2: Looks better (and also works)

Here's an improved way to solve our problem for good:

As we can see, instead of relying to the "double lowercase" approach, we've exploited the Contains() method overload that accept a StringComparison parameter: not only this works well, but it also allows us to use different StringComparison values for different scenarios, thus allowing to choose between applying casing or not depending on the circumstance.

This technique relies upon the Enum type support provided by the System.Linq.Dynamic.Core library, as explained here.

Such strategy is definitely more versatile that the previous one and can be used to create a general purpose helper method to conditionally handle the casing like the following one:

That's definitely the way to go.

Conclusion

That's it, at least for  now: I hope that this post will help other ASP.NET and ASP.NET Core developers looking for a way to use the Contains() method in a case-insensitive way just like they were used to do in older version of Entity Framework.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.