Sy.ExpressionBuilder
1.0.0.3
dotnet add package Sy.ExpressionBuilder --version 1.0.0.3
NuGet\Install-Package Sy.ExpressionBuilder -Version 1.0.0.3
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="Sy.ExpressionBuilder" Version="1.0.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sy.ExpressionBuilder --version 1.0.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sy.ExpressionBuilder, 1.0.0.3"
#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 Sy.ExpressionBuilder as a Cake Addin #addin nuget:?package=Sy.ExpressionBuilder&version=1.0.0.3 // Install Sy.ExpressionBuilder as a Cake Tool #tool nuget:?package=Sy.ExpressionBuilder&version=1.0.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Sy.ExpressionBuilder 使用指南
概述
Sy.ExpressionBuilder 通过模型继承提供灵活查询和分页功能。本指南涵盖模型继承、属性约束、特性使用、排序、分组、分页等功能。
1. 模型继承
模型名称 | 描述 |
---|---|
QueryModel | 基础查询类,适用于简单查询场景 |
PageModel | 分页查询类,默认每页20条数据 |
FullQueryModel | 支持 QueryItems 和 FilterFields 的高级查询 |
FullPageModel | 支持 QueryItems 和 FilterFields 的高级分页 |
注意:FullQueryModel 和 FullPageModel 权限较大,需谨慎使用
2. 属性名约束
属性名规则 | 生成条件 | 示例 |
---|---|---|
以 Id 结尾 |
精确匹配 | UserId → UserId = value |
以 Start/End 结尾 |
时间范围查询 | CreateTimeStart → CreateTime >= value |
以 Min/Max 结尾 |
数字范围查询 | PriceMin → Price >= value |
与表字段一致 | 模糊查询(字符串) | Name → Name.Contains(value) |
3. 特性约束
通过 ConditionAttribute
定义灵活查询条件:
特性参数
PropertyName
: 属性名Condition
: 查询方法(Equal/Contains等)ConditionType
: 查询类型(And/Or)IsSinglePropertyName
: 是否唯一属性名IsCaseSensitive
: 是否区分大小写
使用示例
// 导航父类查询
[Condition($"{nameof(NewType)}.{nameof(NewType.Name)}")]
public string Name { get; set; }
// 导航子类查询
[Condition($"{nameof(NewType)}[{nameof(NewType.Name)}]")]
public string Name { get; set; }
// 自定义条件
[Condition("Role.RoleName", EnumCondition.Equal, EnumConditionType.And)]
public string Name { get; set; }
4. 排序处理
主要方法
方法签名 | 描述 |
---|---|
DefaultOrderBy(string propName, bool isDesc) |
设置默认排序并清空旧条件 |
AddOrderBy(string propName, bool isDesc) |
追加排序条件 |
AddMultipleOrderBy(bool isDesc, params string[] propNames) |
批量添加排序条件 |
示例
queryModel.DefaultOrderBy("CreateTime", true); // 创建时间降序
queryModel.AddOrderBy("Name", false); // 名称升序
5. 分组查询
使用 GroupAttribute
特性:
[Group(1)]
public string Name { get; set; }
[Group(1, EnumConditionType.Or)]
public string UserName { get; set; }
6. 时间跨度查询
使用 DurationAttribute
:
[Duration(3, EnumTimeType.Month)]
public DateTime? CreateTimeStart { get; set; }
public DateTime? CreateTimeEnd { get; set; }
7. 分页配置
参数名 | 描述 |
---|---|
PageIndex | 当前页码 |
PageSize | 每页显示条数 |
8. 参数忽略
使用 NotQueryAttribute
:
[NotQuery]
public string IgnoreField { get; set; }
9. 构建表达式
通过 ToExpression<T>()
构建:
var expression = queryModel.ToExpression<MyEntity>();
10. 查询扩展方法
常用方法
// 过滤数据
var result = query.WhereExt(queryModel).ToList();
// 分页查询
var pageResult = query.ToPageList(pageModel);
// 动态排序
query.OrderBy("CreateTime", isDesc: true);
总结
- 根据场景选择
QueryModel
或PageModel
- 优先使用属性名约束简化代码
- 谨慎使用
FullQueryModel/FullPageModel
- 更多用法参考代码注释和示例项目
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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- Microsoft.CSharp (>= 4.7.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Sy.ExpressionBuilder:
Package | Downloads |
---|---|
Sy.ExpressionBuilder.EFCore
表达式树构造器,EF扩展 |
|
Sy.ExpressionBuilder.Sql
Expression Tree Form Builder, SQL Extension |
GitHub repositories
This package is not used by any popular GitHub repositories.