Skip to content

Commit 2b549a6

Browse files
author
László Jakab
committed
AddQueryFilterExtension
1 parent 280eba0 commit 2b549a6

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

AutSoftCore.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutSoft.AspNetCore.Auth", "
3636
EndProject
3737
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutSoft.AspNetCore.Blazor", "src\AutSoft.AspNetCore.Blazor\AutSoft.AspNetCore.Blazor.csproj", "{1E00D30A-1EF0-4DAD-98C9-61FEE3EA7EF6}"
3838
EndProject
39-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutSoft.Mud.Blazor", "src\AutSoft.Mud.Blazor\AutSoft.Mud.Blazor.csproj", "{91EDCF80-45C8-4728-9240-52DA8DAD8F02}"
39+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutSoft.Mud.Blazor", "src\AutSoft.Mud.Blazor\AutSoft.Mud.Blazor.csproj", "{91EDCF80-45C8-4728-9240-52DA8DAD8F02}"
40+
EndProject
41+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutSoft.EntityFrameworkCore", "src\AutSoft.EntityFrameworkCore\AutSoft.EntityFrameworkCore.csproj", "{A1B22128-F657-474E-ADD1-2CFB6994F14F}"
4042
EndProject
4143
Global
4244
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -80,6 +82,10 @@ Global
8082
{91EDCF80-45C8-4728-9240-52DA8DAD8F02}.Debug|Any CPU.Build.0 = Debug|Any CPU
8183
{91EDCF80-45C8-4728-9240-52DA8DAD8F02}.Release|Any CPU.ActiveCfg = Release|Any CPU
8284
{91EDCF80-45C8-4728-9240-52DA8DAD8F02}.Release|Any CPU.Build.0 = Release|Any CPU
85+
{A1B22128-F657-474E-ADD1-2CFB6994F14F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
86+
{A1B22128-F657-474E-ADD1-2CFB6994F14F}.Debug|Any CPU.Build.0 = Debug|Any CPU
87+
{A1B22128-F657-474E-ADD1-2CFB6994F14F}.Release|Any CPU.ActiveCfg = Release|Any CPU
88+
{A1B22128-F657-474E-ADD1-2CFB6994F14F}.Release|Any CPU.Build.0 = Release|Any CPU
8389
EndGlobalSection
8490
GlobalSection(SolutionProperties) = preSolution
8591
HideSolutionNode = FALSE
@@ -94,6 +100,7 @@ Global
94100
{6B158B44-E28E-4276-BB65-72CDA4163F39} = {C46AA3F7-CBFE-428A-AA49-D1A1D40AB8A6}
95101
{1E00D30A-1EF0-4DAD-98C9-61FEE3EA7EF6} = {C46AA3F7-CBFE-428A-AA49-D1A1D40AB8A6}
96102
{91EDCF80-45C8-4728-9240-52DA8DAD8F02} = {C46AA3F7-CBFE-428A-AA49-D1A1D40AB8A6}
103+
{A1B22128-F657-474E-ADD1-2CFB6994F14F} = {C46AA3F7-CBFE-428A-AA49-D1A1D40AB8A6}
97104
EndGlobalSection
98105
GlobalSection(ExtensibilityGlobals) = postSolution
99106
SolutionGuid = {E4767DF1-41A6-4BF0-A957-2718986D8BC7}

src/AutSoft.All/AutSoft.All.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ProjectReference Include="..\AutSoft.AspNetCore.Auth\AutSoft.AspNetCore.Auth.csproj" />
1111
<ProjectReference Include="..\AutSoft.AspNetCore.Blazor\AutSoft.AspNetCore.Blazor.csproj" />
1212
<ProjectReference Include="..\AutSoft.DbScaffolding.Identity\AutSoft.DbScaffolding.Identity.csproj" />
13+
<ProjectReference Include="..\AutSoft.EntityFrameworkCore\AutSoft.EntityFrameworkCore.csproj" />
1314
<ProjectReference Include="..\AutSoft.Linq\AutSoft.Linq.csproj" />
1415
<ProjectReference Include="..\AutSoft.Mud.Blazor\AutSoft.Mud.Blazor.csproj" />
1516
</ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
2+
using Microsoft.EntityFrameworkCore.Query;
3+
4+
using System.Linq.Expressions;
5+
6+
namespace AutSoft.EntityFrameworkCore.EntityTypeBuilders;
7+
8+
/// <summary>
9+
/// Extension method for <see cref="EntityTypeBuilder"/> to help configure a query filter
10+
/// </summary>
11+
public static class AddQueryFilterExtension
12+
{
13+
/// <summary>
14+
/// Replace the parameters of the <paramref name="expression"/>
15+
/// and after that register it for query filter as an <see cref="LambdaExpression"/>
16+
/// </summary>
17+
/// <typeparam name="T">Type of the parameter of <paramref name="expression"/></typeparam>
18+
/// <param name="entityTypeBuilder">An <see cref="EntityTypeBuilder"/> object</param>
19+
/// <param name="expression">The filter logic of the query filter</param>
20+
public static void AddQueryFilter<T>(this EntityTypeBuilder entityTypeBuilder, Expression<Func<T, bool>> expression)
21+
{
22+
var parameterType = Expression.Parameter(entityTypeBuilder.Metadata.ClrType);
23+
var expressionFilter = ReplacingExpressionVisitor.Replace(
24+
expression.Parameters.Single(), parameterType, expression.Body);
25+
26+
var currentQueryFilter = entityTypeBuilder.Metadata.GetQueryFilter();
27+
if (currentQueryFilter != null)
28+
{
29+
var currentExpressionFilter = ReplacingExpressionVisitor.Replace(
30+
currentQueryFilter.Parameters.Single(), parameterType, currentQueryFilter.Body);
31+
expressionFilter = Expression.AndAlso(currentExpressionFilter, expressionFilter);
32+
}
33+
34+
var lambdaExpression = Expression.Lambda(expressionFilter, parameterType);
35+
entityTypeBuilder.HasQueryFilter(lambdaExpression);
36+
}
37+
}

0 commit comments

Comments
 (0)