DynamicExpression 8.0.12
See the version list below for details.
dotnet add package DynamicExpression --version 8.0.12
NuGet\Install-Package DynamicExpression -Version 8.0.12
<PackageReference Include="DynamicExpression" Version="8.0.12" />
paket add DynamicExpression --version 8.0.12
#r "nuget: DynamicExpression, 8.0.12"
// Install DynamicExpression as a Cake Addin #addin nuget:?package=DynamicExpression&version=8.0.12 // Install DynamicExpression as a Cake Tool #tool nuget:?package=DynamicExpression&version=8.0.12
Dynamic Expression
Easyly, construct lambda expressions dynamically, and turn criteria models into Linq queries. Additionally the library implements a query object model, defining the criteria (optional) and properties for pagination and ordering, to use directly in querying methods.
Feel free to contribute, throw questions and report issues. I usually respond fast (24-48 hours).
Table of Contents:
Query
The query object model has a generic and a non-generic implementations.
The Query
is used when no filtering is required, but pagination and ordering is still needed, while the Query<TCriteria>
is used when custom filter expressions should be applied.
Query Criteria
The query criteria derives from the interface IQueryCriteria
, and implements a single method GetExpressions()
, where TModel
defines the model type of the linq statement the critera expression is converted into. Additionally the query critiera implementation contains properties for each wanted criteria. In the method body of GetExpressions()
build the CriteriaExpression
, defining logical operations and mapping model and criteria properties.
Simple example.
public class MyModel
{
public string MyProperty { get; set; }
}
public class MyQueryCriteria : IQueryCriteria
{
public string MyCriteria { get; set; }
public virtual IList<CriteriaExpression> GetExpressions()
where TEntity : class
{
var expression = new CriteriaExpression();
expression
.Equal("MyProperty", this.MyCriteria);
return new[] { expression };
}
}
Another example, combining two criteria properties to one model property.
public class MyModel
{
public DateTime CreatedAt { get; set; }
}
public class MyQueryCriteria : IQueryCriteria
{
public DateTimeOffset? AfterAt { get; set; }
public DateTimeOffset? BeforeAt { get; set; }
public override IList<CriteriaExpression> GetExpressions()
where TEntity : class
{
var expression = base.GetExpression();
if (this.BeforeAt.HasValue)
{
expression.LessThanOrEqual("CreatedAt", this.BeforeAt);
}
if (this.AfterAt.HasValue)
{
expression.GreaterThanOrEqual("CreatedAt", this.AfterAt);
}
return new[] { expression };
}
}
Criteria Expression Operations
When constructing the CriteriaExpression
the following methods are available.
Note, that not all operations are valid on all data types, but it should be apparent.
- Equal
- NotEqual
- Contains
- Not Contains
- StartsWith
- EndsWith
- GreaterThan
- GreaterThanOrEqual
- LessThan
- LessThanOrEqual
- Between
- IsNull
- IsNotNull
- IsNullOrWhiteSpace
- IsNotNullOrWhiteSpace
- IsEmpty
- IsNotEmpty
Linq Extensions
The library comes with a few IQueryable<T>
extension methods. They serve to convert the CriteriaExpression
returned by the GetExpression()
, into a valid linq expression.
The list below shows the extension methods
IQueryable<T> Order<T>(IOrdering)
IQueryable<T> Limit<T>(IPagination)
IQueryable<T> Where<T>(IQueryCriteria)
Using the IQueryCriteria
implementation from above, applying the criteria expression would look like this.
var criteria = new MyQueryCriteria();
var result = myQueryable
.Where(criteria)
The other extensions methods applies pagination and ordering to the query, IQueryable<T>
.
Query Model Binders
Add Query and Query Criteria to MVC.
services
.AddQueryModelBinders();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DynamicExpression:
Package | Downloads |
---|---|
NanoCore
The project is inspired by years of tedious repetitions, continuously re-writing similar code-snippets and libraries, to handle common functionality, not related to the business domain, such as logging, data persistence, message queuing, documentation, validation and similar. |
GitHub repositories
This package is not used by any popular GitHub repositories.
- Fixed enum arrays didn't work with In and Contains.