FluentCondition 1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package FluentCondition --version 1.0.0
NuGet\Install-Package FluentCondition -Version 1.0.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="FluentCondition" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentCondition" Version="1.0.0" />
<PackageReference Include="FluentCondition" />
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 FluentCondition --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FluentCondition, 1.0.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 FluentCondition@1.0.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=FluentCondition&version=1.0.0
#tool nuget:?package=FluentCondition&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
🚀 FluentCondition
强大的查询条件构建器,让复杂查询变得简单优雅!
✨ 特性
- 🎯 声明式设计 - 使用特性标记定义查询条件,代码清晰易维护
- 🔄 双模式支持 - 同时支持内存查询(Lambda)和数据库查询(SQL)
- 🛡️ 类型安全 - 编译时检查,避免运行时错误
- ⚡ 高性能 - 表达式树编译,零反射性能损耗
- 🔒 安全可靠 - 参数化查询,防止SQL注入
- 🎨 灵活强大 - 支持复杂条件组合、OR逻辑、条件依赖
📦 安装
dotnet add package FluentCondition
或在 Package Manager Console 中:
Install-Package FluentCondition
🎯 快速开始
1. 定义实体类
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
2. 定义查询条件类
using FluentCondition.Attributes;
public class ProductSearch
{
[Equal]
public int? Id { get; set; }
[Contains]
public string Name { get; set; }
[GreaterThanOrEqual(FieldName = "Price")]
public decimal? MinPrice { get; set; }
[In]
public string[] Categories { get; set; }
}
3. 使用条件查询
内存查询(LINQ)
using FluentCondition.Extensions;
var criteria = new ProductSearch { MinPrice = 100 };
var results = products.Where(criteria).ToList();
数据库查询(SQL)
using FluentCondition.Core;
var criteria = new ProductSearch { MinPrice = 100 };
var sql = ConditionBuilder.BuildSqlCondition<Product, ProductSearch>(criteria);
// 输出: WHERE Price >= @p0
Console.WriteLine($"WHERE {sql.WhereClause}");
// 在ADO.NET中使用
using (var command = new SqlCommand(sql.ToSql("SELECT * FROM Product"), connection))
{
command.Parameters.AddRange(sql.Parameters);
// 执行查询...
}
📚 支持的条件类型
| 特性 | 操作符 | 说明 | 示例 |
|---|---|---|---|
[Equal] |
= |
相等 | [Equal] public int? Id |
[NotEqual] |
!= |
不等于 | [NotEqual] public int? Status |
[GreaterThan] |
> |
大于 | [GreaterThan] public int? Age |
[GreaterThanOrEqual] |
>= |
大于等于 | [GreaterThanOrEqual] public decimal? Price |
[LessThan] |
< |
小于 | [LessThan] public int? MaxAge |
[LessThanOrEqual] |
<= |
小于等于 | [LessThanOrEqual] public decimal? MaxPrice |
[Contains] |
LIKE %x% |
包含 | [Contains] public string Name |
[StartsWith] |
LIKE x% |
开头匹配 | [StartsWith] public string Code |
[EndsWith] |
LIKE %x |
结尾匹配 | [EndsWith] public string Suffix |
[In] |
IN (...) |
在列表中 | [In] public string[] Categories |
[IsNull] |
IS NULL |
为空 | [IsNull] public bool? CheckNull |
[IsNotNull] |
IS NOT NULL |
不为空 | [IsNotNull] public bool? CheckNotNull |
🔥 高级特性
OR 条件组合
使用 [OrWith] 特性创建 OR 条件:
public class Search
{
[Contains]
[OrWith(nameof(Email))]
public string Name { get; set; }
[Contains]
[OrWith(nameof(Name))]
public string Email { get; set; }
}
// 生成: (Name LIKE '%search%' OR Email LIKE '%search%')
条件依赖(IF)
使用 [If] 特性根据其他属性决定是否应用条件:
public class Search
{
public bool UseAdvanced { get; set; }
// 只有当 UseAdvanced = true 时才应用此条件
[Equal]
[If(nameof(UseAdvanced), CheckType = ConditionCheck.IsTrue)]
public bool? IsActive { get; set; }
}
支持的检查类型:
ConditionCheck.Equal- 相等ConditionCheck.NotEqual- 不相等ConditionCheck.IsTrue- 为 trueConditionCheck.IsFalse- 为 falseConditionCheck.NotNull- 不为空ConditionCheck.IsNull- 为空
字段名映射
使用 FieldName 属性映射到不同的字段名:
[GreaterThan(FieldName = "Age")]
public int? MinAge { get; set; }
// 映射到 Age 字段,而不是 MinAge
表别名支持
var sql = ConditionBuilder.BuildSqlCondition<Product, ProductSearch>(
criteria,
tableAlias: "p");
// 生成: p.Price >= @p0
📖 完整示例
// 定义查询条件
public class ProductSearchCriteria
{
[Equal]
public int? Id { get; set; }
[Contains]
[OrWith(nameof(Description))]
public string Name { get; set; }
[Contains]
[OrWith(nameof(Name))]
public string Description { get; set; }
[GreaterThanOrEqual(FieldName = "Price")]
public decimal? MinPrice { get; set; }
[LessThanOrEqual(FieldName = "Price")]
public decimal? MaxPrice { get; set; }
[In]
public string[] Categories { get; set; }
public bool UseAdvancedSearch { get; set; }
[GreaterThan(FieldName = "Stock")]
[If(nameof(UseAdvancedSearch), CheckType = ConditionCheck.IsTrue)]
public int? MinStock { get; set; }
}
// 使用示例
var criteria = new ProductSearchCriteria
{
Name = "键盘",
MinPrice = 500,
MaxPrice = 2000,
Categories = new[] { "电子产品", "配件" },
UseAdvancedSearch = true,
MinStock = 10
};
// Lambda 查询
var memoryResults = products.Where(criteria).ToList();
// SQL 查询
var sql = ConditionBuilder.BuildSqlCondition<Product, ProductSearchCriteria>(criteria);
Console.WriteLine(sql.ToSql("SELECT * FROM Product"));
// 输出: SELECT * FROM Product WHERE (Name LIKE @p0 OR Description LIKE @p1)
// AND Price >= @p2 AND Price <= @p3 AND Categories IN (@p4, @p5) AND Stock > @p6
🧪 运行示例项目
cd samples/FluentCondition.Samples
dotnet run
🧪 运行测试
dotnet test
📄 许可证
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情
🤝 贡献
欢迎提交 Issue 和 Pull Request!
📧 联系方式
如有问题或建议,请通过 GitHub Issues 联系我们。
⭐ 如果这个项目对您有帮助,请给我们一个 Star!
| Product | Versions 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. 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. |
| .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
- Microsoft.Data.SqlClient (>= 6.1.3)
- MySql.Data (>= 9.5.0)
- System.Reflection.Extensions (>= 4.3.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on FluentCondition:
| Package | Downloads |
|---|---|
|
FluentCondition.MySql
FluentCondition MySQL extension package for generating MySQL SQL WHERE clauses. |
|
|
FluentCondition.SqlServer
FluentCondition SQL Server extension package for generating SQL Server SQL WHERE clauses. |
GitHub repositories
This package is not used by any popular GitHub repositories.