Skip to content

Commit 0179f13

Browse files
committed
SQLite workaround for case insensitive filtering
1 parent 148b398 commit 0179f13

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

aspnetcore/blazor/tutorials/movie-database-app/part-6.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ Change the `QuickGrid` component's <xref:Microsoft.AspNetCore.Components.QuickGr
5858

5959
The `movie => movie.Title!.Contains(...)` code is a *lambda expression*. Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as the <xref:System.Linq.Queryable.Where%2A> or <xref:System.String.Contains%2A> methods. LINQ queries aren't executed when they're defined or when they're modified by calling a method, such as <xref:System.Linq.Queryable.Where%2A>, <xref:System.String.Contains%2A>, or <xref:System.Linq.Queryable.OrderBy%2A>. Rather, query execution is deferred. The evaluation of an expression is delayed until its realized value is iterated.
6060

61-
The <xref:System.Data.Objects.DataClasses.EntityCollection%601.Contains%2A> method is run on the database, not in the C# code. The case sensitivity of the query depends on the database and the collation. For SQL Server, <xref:System.String.Contains%2A> maps to [SQL `LIKE`](/sql/t-sql/language-elements/like-transact-sql), which is case insensitive. SQLite with default collation provides a mixture of case sensitive and case insensitive filtering, depending on the query. For information on making case insensitive SQLite queries, see the [Additional resources](#additional-resources) section of this article.
61+
The <xref:System.Data.Objects.DataClasses.EntityCollection%601.Contains%2A> method is run on the database, not in the C# code. The case sensitivity of the query depends on the database and the collation. For SQL Server, <xref:System.String.Contains%2A> maps to [SQL `LIKE`](/sql/t-sql/language-elements/like-transact-sql), which is case insensitive. SQLite with default collation provides a mixture of case sensitive and case insensitive filtering, depending on the query. The remainder of this tutorial assumes case insensitive database collation. As a workaround for case sensitive database collation with SQLite, you can adjust the Linq query to lowercase the filtering operation by applying <xref:System.String.ToLower%2A> to the movie title and the filter variable. The following workaround is purely for demonstration purposes to match the instructions and examples in this tutorial:
62+
63+
```diff
64+
- m => m.Title!.Contains(TitleFilter ?? string.Empty)
65+
+ m => m.Title!.ToLower().Contains((TitleFilter ?? string.Empty).ToLower());
66+
```
67+
68+
For information on the best practices to adopt case insensitive SQLite queries, see the [Additional resources](#additional-resources) section of this article.
6269

6370
Run the app and navigate to the movies `Index` page at `/movies`. The movies in the database load:
6471

aspnetcore/blazor/tutorials/movie-database-app/part-8.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ private IQueryable<Movie> FilteredMovies =>
172172
context.Movie.Where(m => m.Title!.Contains(titleFilter));
173173
```
174174

175+
> [!NOTE]
176+
> If your database collation is case sensitive (for example, when using a SQLite database), you can lowercase the movie's title and `titleFilter` as a workaround:
177+
>
178+
> ```diff
179+
> - m => m.Title!.Contains(titleFilter)
180+
> + m => m.Title!.ToLower().Contains(titleFilter.ToLower())
181+
> ```
182+
>
183+
> For information on the best practices to adopt case insensitive SQLite queries, see the [Additional resources](#additional-resources) section of this article.
184+
175185
Next, the component should bind the `titleFilter` field to an `<input>` element, so user input is stored in the `titleFilter` variable. Binding is achieved in Blazor with the `@bind` directive attribute.
176186
177187
Remove the HTML form from the component:
@@ -295,3 +305,11 @@ In the documentation website's sidebar navigation, articles are organized by sub
295305

296306
> [!div class="step-by-step"]
297307
> [Previous: Add a new field](xref:blazor/tutorials/movie-database-app/part-7)
308+
309+
## Additional resources
310+
311+
Information and best practices to adopt case insensitive SQLite queries:
312+
313+
* [How to use case-insensitive query with Sqlite provider? (`dotnet/efcore` #11414)](https://github.com/dotnet/efcore/issues/11414)
314+
* [How to make a SQLite column case insensitive (`dotnet/AspNetCore.Docs` #22314)](https://github.com/dotnet/AspNetCore.Docs/issues/22314)
315+
* [Collations and Case Sensitivity](/ef/core/miscellaneous/collations-and-case-sensitivity)

0 commit comments

Comments
 (0)