AdaptiveSearch 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package AdaptiveSearch --version 1.0.6                
NuGet\Install-Package AdaptiveSearch -Version 1.0.6                
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="AdaptiveSearch" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AdaptiveSearch --version 1.0.6                
#r "nuget: AdaptiveSearch, 1.0.6"                
#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 AdaptiveSearch as a Cake Addin
#addin nuget:?package=AdaptiveSearch&version=1.0.6

// Install AdaptiveSearch as a Cake Tool
#tool nuget:?package=AdaptiveSearch&version=1.0.6                

AdaptiveSearch

AdaptiveSearch is a lightweight and powerful library for dynamically building LINQ filtering expressions in C#. It supports dynamic filtering for strings, numbers, and DateTime properties, making it ideal for advanced search features, APIs, and reporting systems.


Features

  • String Filtering: Supports Equal, NotEqual, StartsWith, EndsWith, Contains, and their negative counterparts.
  • Numeric Filtering: Compare values using Equal, NotEqual, GreaterThan, LessThan, GreaterThanOrEqual, and LessThanOrEqual.
  • DateTime Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • TimeSpan Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • IntegerFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • DoubleFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • FloatFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • DecimalFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • LongFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • ShortFilter Filtering: Dynamically compare dates using Equal, NotEqual, GreaterThan, LessThan, and similar conditions.
  • Type-Safe: Avoids magic strings with lambda expressions for property selection.
  • Dynamic and Extensible: Easily integrate and combine filters for complex queries.
  • LINQ Compatible: Seamlessly integrates with LINQ-to-Objects, Entity Framework, and other query providers.

Installation

Install AdaptiveSearch via NuGet:

dotnet add package AdaptiveSearch

Usage

1. Single Filter

var stringFilter = new StringFilter
{
    StartsWith = "John",
    NotContains = "Doe"
};

var data = new List<Person>
{
    new Person { Name = "John Smith" },
    new Person { Name = "Johnny Doe" },
    new Person { Name = "Jane Doe" }
}.AsQueryable();

var result = data.AdaptiveSearch(x => x.Name, filter).ToList();

foreach (var person in result)
{
    Console.WriteLine(person.Name);
}

Output:

John Smith

1. Apply filter to multi property

var filter = new StringFilter { NotStartsWith = "S" };

var data = new List<Person>
{
  new() { Name = "John Doe",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20 ,JobTitle="Software Engineer",Specialty="IT" },
  new() { Name = "Johnny",CreatedAt=new DateTime(2025,1,1),IsActive=true,Age=25,JobTitle="Software Tester",Specialty="IT" },
  new() { Name = "Jane",CreatedAt=new DateTime(2024,1,1),IsActive=false,Age=30,JobTitle="HR Manager",Specialty="HR" },
  new() {Name ="John",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=21,JobTitle="Software Qc",Specialty="IT" },
  new(){ Name="Tom",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20,JobTitle="Software Engineer",Specialty="IT" },
  new() { Name = "Sally", CreatedAt = new DateTime(2024, 1, 1), IsActive = false, Age = 30, JobTitle = "HR Manager", Specialty = "HR" }
}.AsQueryable();

var result1 = data.AdaptiveSearch(filter, ApplyType.And)
          .ApplyTo(c => c.Name)
          .ApplyTo(y => y.JobTitle)
          .ApplyTo(t => t.Specialty)
          .ToList(); // result is only Jane

var result1 = data.AdaptiveSearch(filter, ApplyType.Or)
          .ApplyTo(c => c.Name)
          .ApplyTo(y => y.JobTitle)
          .ApplyTo(t => t.Specialty)
          .ToList(); // Return all data as all of them passed

2. Multi Filter


public class Filters 
{
    public StringFilter? Name { get; set; }
    public IntegerFilter? Age { get; set; }
    public DateTimeFilter? CreatedAt { get; set; }
    public BooleanFilter? IsActive { get; set; }
    }

/// generate filters instance
var filter = new Filters
{
    Name = new() { StartsWith = "John" },
    Age = new() { Equal = 20 },
    IsActive = new() { Equal = true },
    CreatedAt = new()
    {
        Equal = new DateTime(2023, 1, 1)
    }
};

var data = new List<Person>
{
     new() { Name = "John Doe",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20 },
     new() { Name = "Johnny",CreatedAt=new DateTime(2025,1,1),IsActive=true,Age=25 },
     new() { Name = "Jane",CreatedAt=new DateTime(2024,1,1),IsActive=false,Age=30 },
     new() {Name ="John",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=21 },
     new(){ Name="Tom",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20 }
}.AsQueryable();

var result = data.AdaptiveSearch(filter).ToList();

foreach (var person in result)
{
    Console.WriteLine(person.Name);
}

Output:

John Doe

2. Apply all filters

By default calling AdaptiveSearch apply only properties that implement IAdaptiveFilter if you need equal operator to be applied to other types you need to pass applyAllProperties parameter as true


public class Filters 
{
    public string? Name { get; set; }
    }

/// generate filters instance
var filter = new Filters
{
    Name = "John",
};

var data = new List<Person>
{
     new() { Name = "John Doe",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20 },
     new() { Name = "Johnny",CreatedAt=new DateTime(2025,1,1),IsActive=true,Age=25 },
     new() { Name = "Jane",CreatedAt=new DateTime(2024,1,1),IsActive=false,Age=30 },
     new() {Name ="John",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=21 },
     new(){ Name="Tom",CreatedAt=new DateTime(2023,1,1),IsActive=true,Age=20 }
}.AsQueryable();

var result = data.AdaptiveSearch(filter,true).ToList();

foreach (var person in result)
{
    Console.WriteLine(person.Name);
}

Output:

John

Skip and take

you can also add skip and take properties:

[Skip]
public int Skip { get; set; }
[Take]
public int Take { get; set; } = 20;
/// Paging is not applied by default
/// You can apply paging either by calling ApplyPaging() or pass ApplyPaging as and argument to adaptive search

Currently we apply skip then take at the end of query


API Documentation

StringFilter

Property Description
Equal Checks if the value equals a given string.
NotEqual Checks if the value does not equal a given string.
StartsWith Checks if the string starts with the specified value.
EndsWith Checks if the string ends with the specified value.
Contains Checks if the string contains the specified value.
NotContains Ensures the string does not contain the value.

ComparableFilter<T>

Property Description
Equal Checks if the value equals a specified number.
NotEqual Checks if the value does not equal a number.
GreaterThan Checks if the value is greater than the specified number.
LessThan Checks if the value is less than the specified number.
GreaterThanOrEqual Ensures the value is greater than or equal to a number.
LessThanOrEqual Ensures the value is less than or equal to a number.

note you can use this with any type delivered from IComparable


BooleanFilter

Property Description
Equal Filters for dates equal to the given value.
NotEqual Filters out dates equal to the given value.

EnumFilter

Property Description Example
Equal Matches a specific enum value. Equal = Status.Active
NotEqual Excludes a specific enum value. NotEqual = Status.Inactive
In Matches if the value is in a list of enum values. In = new List<Status> { Active, Suspended }
NotIn Excludes values that are in a list of enum values. NotIn = new List<Status> { Deleted }

ListFilter

Property Description Example
ContainsAny Matches if any value in the target property exists in the provided list. ContainsAny = new List<string> { "C#", "LINQ" }
ContainsAll Matches if all values in the provided list exist in the target property. ContainsAll = new List<string> { "C#", "ASP.NET" }
DoesNotContainAny Excludes items where any value in the target property exists in the provided list. DoesNotContainAny = new List<string> { "Outdated" }
DoesNotContainAll Excludes items where all values in the provided list exist in the target property. DoesNotContainAll = new List<string> { "Deprecated", "Legacy" }

Benefits

  • Flexible: Supports dynamic queries for strings, numbers, and dates.
  • Type-Safe: Leverages lambda expressions for strong typing.
  • Extensible: Easily combine filters for complex search scenarios.
  • Framework-Compatible: Works with LINQ and Entity Framework.

Contributing

Contributions are welcome! If you find an issue or want to add a feature, please open an issue or submit a pull request.


License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

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.10.0 130 12/23/2024
1.0.9 89 12/21/2024
1.0.8 86 12/21/2024
1.0.7 83 12/21/2024
1.0.6 81 12/20/2024
1.0.5 80 12/20/2024
1.0.4 84 12/20/2024
1.0.3 81 12/20/2024
1.0.2 94 12/17/2024
1.0.1 84 12/17/2024
1.0.0 78 12/17/2024