XSpecification.Linq 1.1.0

dotnet add package XSpecification.Linq --version 1.1.0
                    
NuGet\Install-Package XSpecification.Linq -Version 1.1.0
                    
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="XSpecification.Linq" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XSpecification.Linq" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="XSpecification.Linq" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XSpecification.Linq --version 1.1.0
                    
#r "nuget: XSpecification.Linq, 1.1.0"
                    
#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.
#:package XSpecification.Linq@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XSpecification.Linq&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=XSpecification.Linq&version=1.1.0
                    
Install as a Cake Tool

XSpecification is an implementation of Specification pattern for Linq and Elasticsearch

Implemented filters: RangeFilter (BETWEEN x AND y), ListFilter (IN(x,y,z)), StringFilter (=, LIKE '%x%'), direct comparison, NULL/NOT NULL

This library helps with removing boilerplate code and adds commonly used filtering capabilities, especially if your app has many grids with similar filtering capabilities. It could be useful in BL-heavy scenarios when you find yourself writing code like this:

public class SomeApiFilter
{
    public DateTime? Date { get;set }
    public string Name { get;set }
    public string NameContains { get;set }
    public int? IdFrom { get; set; }
    public int? IdTo { get; set; }
}

var filter = new SomeApiFilter
    {
        Date = DateTime.Today,
        NameContains = "complex",
        IdFrom = 0,
        IdTo = 5
    };

var where = PredicateBuilder.New<DbModel>();
if (filter.Date.HasValue)
{
    where.And(f => f.Date == filter.Date.Value);
}
if (!string.IsNullOrEmpty(filter.Name))
{
    where.And(f => f.Name == filter.Name);
}
if (!string.IsNullOrEmpty(filter.NameContains))
{
    where.And(f => f.Name.Contains(filter.NameContains));
}
if (filter.IdFrom.HasValue)
{
    where.And(f => f.Id >= filter.IdFrom);
}
if (filter.IdTo.HasValue)
{
    where.And(f => f.Id <= filter.IdTo);
}

var data = dbcontext.Set<DbModel>().Where(where);

With XSpecification it becomes this:

var expression = spec.CreateFilterExpression(filter);
var data = dbcontext.Set<DbModel>().Where(expression);

Setup

Formalize your filter

public class LinqTestFilter
{
   public int Id { get; set; }
   public StringFilter Name { get; set; }
   public string Explicit { get; set; }
   public bool Conditional { get; set; }
}

Create your specification, if a filter property has the same name as in DB model it will be mapped automatically. More examples in the test project.

public class LinqTestSpec : SpecificationBase<LinqTestModel, LinqTestFilter>
{
    public LinqTestSpec(ILogger<LinqTestSpec> logger, IOptions<Options> options)
        : base(logger, options)
    {
        IgnoreField(f => f.Ignored);
        HandleField(f => f.Explicit, m => m.UnmatchingProperty);
        HandleField(f => f.Conditional, (prop, filter) =>
        {
            if (filter.Conditional)
            {
                return CreateExpressionFromFilterProperty(prop, f => f.Name, filter.Conditional.ToString());
            }

            if(!filter.Conditional && filter.Id == 312)
            {
                return PredicateBuilder.New<LinqTestModel>()
                                       .And(f => f.Date.Hour == 1)
                                       .And(f => f.UnmatchingProperty == 123);
            }

            return DoNothing;
        });
    }
}

Add XSpecification to your DI in Startup.cs or Program.cs

services.AddLinqSpecification();
//or optionally disable convention-based property handling
services.AddLinqSpecification(o =>
            {
                o.DisableAutoPropertyHandling = true;
            });
// add specification to your DI
services.AddSingleton<LinqTestSpec>();

Using in your code

// Inject from DI
var spec = serviceProvider.GetRequiredService<LinqTestSpec>();

var filter = new LinqTestFilter
{
    Date = DateTime.Today, // Date = 'xxxx-xx-xx....'
    Id = 123, // Id = 123
    Name = "qwe", // Name = 'qwe'
    ComplexName = new StringFilter("complex") { Contains = true }, // ComplexName LIKE '%complex%'
    ListDate = new[] { DateTime.Today, DateTime.Today.AddDays(1) }, // ListDate IN ('xxxx-xx-xx....', 'xxxx-xx-xx....')
    ListId = new[] { 1, 2, 3 }, // ListId IN (1, 2, 3)
    ListName = new ListFilter<string>("a", "b", "z") { IsInverted = true }, // ListName NOT IN ('a', 'b', 'b')
    NullableDate = DateTime.Today.AddDays(-1), // NullableDate = 'xxxx-xx-xx....'
    RangeDate = new RangeFilter<DateTime> { Start = DateTime.Today, End = DateTime.Today.AddDays(1) }, // RangeDate >= 'xxxx-xx-xx....' AND RangeDate <= 'yyyy-yy-yy....'
    RangeId = new RangeFilter<int> { Start = 0, End = 5 } // RangeId >= 0 AND RangeId <= 5
};

var expression = spec.CreateFilterExpression(filter);
var data = dbcontext.Set<LinqTestModel>().Where(expression);

/* will be translated to
SELECT ... FROM LinqTestModel
WHERE Date = 'xxxx-xx-xx....' AND Id = 123 AND ComplexName LIKE '%complex%' AND ListDate IN ('xxxx-xx-xx....', 'xxxx-xx-xx....')
AND ListId IN (1, 2, 3) AND ListName NOT IN ('a', 'b', 'b') AND NullableDate = 'xxxx-xx-xx....'
AND RangeDate >= 'xxxx-xx-xx....' AND RangeDate <= 'yyyy-yy-yy....' AND RangeId >= 0 AND RangeId <= 5
*/

TODO

  • Add extension points for new filter types
  • Refactor Expression creation logic to Chain of responsipility
  • Add Elasticsearch implementation
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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 85 4/29/2026
1.0.0 474 4/19/2022