dotnet-daily-tips

061 - EF.Functions

Entity Framework provides a convenient abstraction for accessing databases. When combined with LINQ, it offers a robust framework for data access. When executed, LINQ queries are converted into SQL queries based on the code. However, not all expressions written directly in LINQ can be translated into database operations. In such cases, we need to use additional helpers.

EF.Functions offer several common operations that cannot be accessed otherwise.

Docs πŸ“‘: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbfunctions?view=efcore-8.0

These are just a few examples. It’s important to note that these functions may not be compatible if we switch to a different database engine.

// βœ… Functions.Like allows to use SQL LIKE operator
var lessons = dbContext.Lessons.Where(x => EF.Functions.Like(x.Title, "%NET%"));