![]() |
VOOZH | about |
dotnet add package DynamicExpression --version 10.0.2
NuGet\Install-Package DynamicExpression -Version 10.0.2
<PackageReference Include="DynamicExpression" Version="10.0.2" />
<PackageVersion Include="DynamicExpression" Version="10.0.2" />Directory.Packages.props
<PackageReference Include="DynamicExpression" />Project file
paket add DynamicExpression --version 10.0.2
#r "nuget: DynamicExpression, 10.0.2"
#:package DynamicExpression@10.0.2
#addin nuget:?package=DynamicExpression&version=10.0.2Install as a Cake Addin
#tool nuget:?package=DynamicExpression&version=10.0.2Install as a Cake Tool
👁 Build and Deploy
👁 NuGet
👁 NuGet
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).
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.
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 };
}
}
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.
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>.
Ordering supports using '.' to navigate to nested properties.
Add Query and Query Criteria to MVC.
services
.AddQueryModelBinders();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on DynamicExpression:
| Package | Downloads |
|---|---|
|
Nano.Data.Abstractions
This package is part of the Nano Library, a set of reusable .NET libraries for building microservice applications. Nano addresses common non-business concerns such as logging, persistence, messaging, validation, and documentation, while remaining fully configurable and extensible, so applications can stay focused on business logic. See https://github.com/Nano-Core/Nano.Library for details. |
This package is not used by any popular GitHub repositories.
- Fixed The QueryModelBinder for enum values in querystring criteria.