Skip to main content
.NETStandard2.0

Query filters for linq expressions

Published
Jun 14, 2024
2y ago
Total Downloads
3.3K
3,328

Install

dotnet add package aqua-accesscontrol --version 1.0.0-beta-001
ProductVersions
.NET Standard
netstandard2.0

README

About

This package provides extension methods for System.Linq.IQueryable<> and System.Linq.Expressions.Expression types, to apply query filters at various levels.

Features

Global Predicate

Global predicates apply to a query as a whole and must be satisfied for any results be returned.

Predicates can be based on progam logic and/or on expanded data query:

// base query
var query =
    from p in repo.Products
    select p;
// code based predicate
var result1 = query
    .Apply(Predicate.Create(() => true))
    .ToList();
// predicate based on data qeury
var result2 = query
    .Apply(Predicate.Create(() =>
        repo.Claims.Any(c =>
            c.Type == ClaimTypes.Tenant &&
            c.Value == "1" &&
            c.Subject == username)))
    .ToList();

Type Predicate

Type predicates apply to specific record types within a query by filtering out corresponding records that do not satisfy the condition.

The following predicate filters out records which have not TenantId equal to 1:

var query =
    from o in repo.Orders
    select new { o.Id };
var result = query
    .Apply(Predicate.Create<Order>(o => o.TenantId == 1))
    .ToList();

Property Predicate

Property predicates do not filter out records but allow property values to be returned only when specified conditions are satisfied.

The following predicate retrieves product prices only for records which have TenantId equal to 1, other records have the Price property set to its default vaule:

var query =
    from p in repo.Products
    select new { p.Id, p.Price };
var result = query
    .Apply(Predicate.Create<Product, decimal>(p => p.Price, p => p.TenantId == 1))
    .ToList();

Property Projection Predicate

Property projection predicates allow to project values of a certain property based on custom logic.

In the following example, a 10% discount is applied if TenantId is equal to 1:

var query =
    from p in repo.Products
    select new { p.Id, p.Price };

var result = query
    .Apply(Predicate.CreatePropertyProjection<Product, decimal>(
        p => p.Price,
        p => p.TenantId == 1 ? p.Price * 0.9m : p.Price))
    .ToList();

Changes from Previous Version

v0.1.0-alpha-004v1.0.0-beta-001
Added Dependencies
Microsoft.Bcl.HashCode(.NETStandard2.0)

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

This version is marked prerelease. Validate its API and upgrade path before production use.No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.