MatSqlFilter 1.0.4

dotnet add package MatSqlFilter --version 1.0.4
                    
NuGet\Install-Package MatSqlFilter -Version 1.0.4
                    
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="MatSqlFilter" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MatSqlFilter" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="MatSqlFilter" />
                    
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 MatSqlFilter --version 1.0.4
                    
#r "nuget: MatSqlFilter, 1.0.4"
                    
#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 MatSqlFilter@1.0.4
                    
#: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=MatSqlFilter&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=MatSqlFilter&version=1.0.4
                    
Install as a Cake Tool

SqlFilter

Overview

SqlFilter is a simple and flexible library that generates SQL WHERE clauses dynamically using conditions and logical operators. It supports nested filters and allows complex query building.

Features

  • Supports multiple condition types (Equals, NotEquals, Like, Contains, etc.)
  • Allows logical operators (AND, OR)
  • Supports nested filtering using recursion
  • Generates SQL-friendly query strings

Installation

This is a standalone C# class. Simply include SqlFilter.cs in your project.

Usage

Sample Code

var c1 = new Condition("Name", "John,xd", nameof(FilterType.Contains));
var c2 = new Condition("Age", "25", nameof(FilterType.GreaterThan));
var c3 = new Condition("City", "New York", nameof(FilterType.Equals));
var c4 = new Condition("Country", "USA1", nameof(FilterType.Equals));
var c5 = new Condition("Country", "USA2", nameof(FilterType.Equals));
var c6 = new Condition("Country", "USA3", nameof(FilterType.Equals));
var c7 = new Condition("Country", "USA4", nameof(FilterType.Equals));
var c8 = new Condition("Country", "USA5", nameof(FilterType.NotEquals));

// Creating logical groups
var group1 = new Filter(new List<IFilterComponent> { c1, c2 }, "OR");
var group2 = new Filter(new List<IFilterComponent> { c3, c4 }, "AND");
var group3 = new Filter(new List<IFilterComponent> { c5, c6 }, "OR");
var group4 = new Filter(new List<IFilterComponent> { c7, c8 }, "AND");

// Nesting filters
var nestedFilter1 = new Filter(new List<IFilterComponent> { group1, group2 }, "AND");
var nestedFilter2 = new Filter(new List<IFilterComponent> { group3, group4 }, "OR");

var finalFilter = new Filter(new List<IFilterComponent> { nestedFilter1, nestedFilter2 }, "AND");

// Generating SQL WHERE clause
Console.WriteLine(SqlFilter.GenerateFiltr(finalFilter).Item2);

Expected Output

((Name IN ('John,xd') OR Age > '25') AND (City = 'New York' AND Country = 'USA1')) AND ((Country = 'USA2' OR Country = 'USA3') OR (Country = 'USA4' AND Country <> 'USA5'))

Filter Operators

  • Equals → Column = 'Value'
  • NotEquals → Column <> 'Value'
  • Like → Column LIKE '%Value%'
  • Contains → Column IN (Value1, Value2, ...)
  • GreaterThan → Column > 'Value'
  • LessThan → Column < 'Value'

Logical Operators

  • AND
  • OR

Notes

  • Nested filters allow building complex WHERE conditions.
  • Ensure that the logic operator is correctly set (either AND or OR).
  • Avoid SQL injection by using parameterized queries when integrating this logic into a real database system.

License

MIT License. Free to use and modify.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 was computed.  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.
  • net9.0

    • 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.0.4 270 3/9/2025
1.0.3 232 3/9/2025
1.0.2 240 3/9/2025