Attributes and enums for BlogDoFT EF Predicate Source Generator.
$ dotnet add package BlogDoFT.Libs.EntityFramework.CodeGenerator.AbstractionsStrongly-typed attributes and enums used by the Predicate Generator.
These annotations are applied to filter DTOs so a source generator can emit ToPredicate() and HasFilter() methods.
Target Frameworks:
netstandard2.0
C# Language Version: C# 11+ (required for generic attributes)
GeneratePredicateAttribute<TEntity>StringFilterAttributeNumericFilterAttributeTemporalFilterAttributeBooleanFilterAttributeComparisonOperator enumInstall it in the app project:
dotnet add package BlogDoFT.Libs.EntityFramework.CodeGenerator.Abstractions
Annotate your filter DTO with [GeneratePredicate<TEntity>] and mark each filterable property with the respective attribute.
using BlogDoFT.Libs.EntityFramework.CodeGenerator.Abstractions;
[GeneratePredicate<Domain.UserRecord>]
public partial class UserFilterDto
{
[StringFilter(TargetProperty = nameof(Domain.UserRecord.Name), Order = 1)]
public string? Name { get; init; }
[StringFilter(TargetProperty = nameof(Domain.UserRecord.Email), Order = 2)]
public string? Email { get; init; }
[NumericFilter(TargetProperty = nameof(Domain.UserRecord.Age),
Operator = ComparisonOperator.GreaterThanOrEqual, Order = 3)]
public int? MinimumAge { get; init; }
[TemporalFilter(TargetProperty = nameof(Domain.UserRecord.CreatedAt),
Operator = ComparisonOperator.LessThanOrEqual, Order = 4)]
public DateTime? CreatedUntil { get; init; }
[BooleanFilter(TargetProperty = nameof(Domain.UserRecord.Active), Order = 5)]
public bool? Active { get; init; }
}
The DTO must be
partial. The generator adds the methods into the same type.
Generated methods
Expression<Func<TEntity, bool>> ToPredicate()bool HasFilter()HasFilter() returns true if at least one annotated property holds a non-null value.
ToPredicate() returns entity => true when all annotated values are null; otherwise it combines only the provided filters with logical AND.
When the string value contains %, the pattern is passed to the database (LIKE):
% → equality (field == value)% → database pattern (e.g., value%)% → database pattern (e.g., %value)% in the middle or both sides → database pattern (e.g., %val%ue%)This relies on the generator’s provider-aware output (see the Generator README).
NumericFilterAttribute and TemporalFilterAttribute accept Operator: ComparisonOperator:
Equal, GreaterThan, LessThan, GreaterThanOrEqual, LessThanOrEqual.Always compared by equality.
Usually, the where field orders matters - for database index usage.
Filters are composed by Order (if specified). Otherwise, DTO declaration order is used.
C# 11+ (for generic attributes)
Works with EF Core 6/7/8+ (generator handles provider-specific differences)