DynamicExpression 8.0.15

dotnet add package DynamicExpression --version 8.0.15                
NuGet\Install-Package DynamicExpression -Version 8.0.15                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DynamicExpression" Version="8.0.15" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DynamicExpression --version 8.0.15                
#r "nuget: DynamicExpression, 8.0.15"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DynamicExpression as a Cake Addin
#addin nuget:?package=DynamicExpression&version=8.0.15

// Install DynamicExpression as a Cake Tool
#tool nuget:?package=DynamicExpression&version=8.0.15                

Dynamic Expression

Build status 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).

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>.
Ordering supports using '.' to navigate to nested properties.

Query Model Binders

Add Query and Query Criteria to MVC.

services
    .AddQueryModelBinders();
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
8.0.15 425 11/4/2024
8.0.14 83 11/3/2024
8.0.13 101 11/2/2024
8.0.12 72 11/2/2024
8.0.11 422 10/21/2024
8.0.10 204 8/29/2024
8.0.9 1,341 7/28/2024

- Cosmetics.