Skip to content

PaulTrampert/PTrampert.QueryObjects

Repository files navigation

PTrampert.QueryObjects

NuGet Version

Attribute-based query objects for LINQ/IQueryable in .NET

Overview

PTrampert.QueryObjects is a .NET library that enables you to define query objects using attributes, making it easy to build dynamic, type-safe queries for IQueryable<T> sources. By annotating properties on your query object, you can declaratively specify how each property should filter your data, and then apply the query object to any IQueryable<T> using a simple extension method.

Features

  • Attribute-based query object filtering
  • Supports custom query logic via IQueryObject<T>
  • Works with any IQueryable<T> (e.g. Entity Framework, MongoDB) or IEnumerable<T>

Example Usage

Suppose you have a target class and a query object:

public class Person
{
    public int Age { get; set; }
    public string Name { get; set; } = string.Empty;
}

public class PersonQuery
{
    [EqualsQuery]
    public int? Age { get; set; }

    [StringContainsQuery]
    public string? Name { get; set; }
}

You can use your query object to filter an IQueryable<Person>:

using PTrampert.QueryObjects;

var people = new List<Person>
{
    new Person { Age = 30, Name = "Alice" },
    new Person { Age = 40, Name = "Bob" },
    new Person { Age = 30, Name = "Caroline" }
};

var query = new PersonQuery { Age = 30, Name = "li" };

var filtered = people.Where(query).ToList();
// filtered contains Alice and Caroline

Full documentation can be found here

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Define IQueryable Where clauses with POCO's.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages