An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

[![Build Status](https://frederikgheysels.visualstudio.com/GitHub%20Pipelines/_apis/build/status/Fg.EFCore.QueryExtensions%20CI?branchName=master)](https://frederikgheysels.visualstudio.com/GitHub%20Pipelines/_build/latest?definitionId=2&branchName=master)
[![NuGet Badge](https://buildstats.info/nuget/Fg.EfCore.QueryExtensions)](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.