https://github.com/fgheysels/fg.efcore.queryextensions
Project which contains helpers and extensions for querying a relational database using EF Core
https://github.com/fgheysels/fg.efcore.queryextensions
efcore entity-framework-core
Last synced: about 2 months ago
JSON representation
Project which contains helpers and extensions for querying a relational database using EF Core
- Host: GitHub
- URL: https://github.com/fgheysels/fg.efcore.queryextensions
- Owner: fgheysels
- Created: 2020-06-18T20:21:41.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-19T09:05:31.000Z (over 3 years ago)
- Last Synced: 2025-01-30T00:46:01.436Z (4 months ago)
- Topics: efcore, entity-framework-core
- Language: C#
- Homepage:
- Size: 21.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
[](https://frederikgheysels.visualstudio.com/GitHub%20Pipelines/_build/latest?definitionId=2&branchName=master)
[](https://www.nuget.org/packages/Fg.EfCore.QueryExtensions/)# Abstract
This project contains some extensions to EF Core (3)
It seems not to be possible to create a LINQ expression in Entity Framework Core which translates into a SQL `LIKE` exprression which matches against multiple possible values. That's why I've decided to create this project and make it available via NuGet.
# Installation
Get it via NuGet:
```
PM> Install-Package Fg.EfCore.QueryExtensions
```# Features
## Generating a LIKE expression against multiple possible values
It seems not to be possible to create a LINQ expression in Entity Framework Core 3 which translates into a SQL clause that looks like this:
```sql
WHERE ([name] LIKE 'foo%' OR [name] LIKE 'bar%')
```I thought the above clause could be generated with an expression that looks like the one below, but apparently, that is not true
```csharp
var names = new string[] {"foo%", "bar%"};var results = dbContext.Persons.Where( p => names.Any(n => p.Name.Startswith(n)));
```This project contains a helper methods which allows you to write this LINQ query:
```csharp
var names = new string[] {"foo%", "bar%"};var results = await dbContext.Persons.Where(DbFilterExpression.LikeOneOf(nameof(Person.Name), names)).ToListAsync();
```## Pagination / Create paged query results
An extension method is available which allows to easily create paged query results:
```csharp
DataPage result = dbContext.Persons.ToPagedResultAsync(pageNumber: 1, pageSize: 20);
```This is an extension method on `IQueryable` so you're able to use it filtered expessions as well:
```csharp
DataPage result = dbContext.Persons.Where(p => p.Name.Startswith("Fre")).ToPagedResultAsync(pageNumber: 1, pageSize: 20);
```> The `ToPagedResult` extension method will execute the query so it is important to have this method as the last part of your query expression.