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 使用说明
1.该表达式插件通过模型继承获取对应的能力。目前提供的模型有
- QueryModel :基础查询类
- PageModel:分页查询参数类(默认每页分页20条)
- FullQueryModel:查询模型,对比QueryModel 多了查询集合QueryItems和过滤FilterFields。前端可以在后端给与的基础上,添加查询参数,对于前端来说权限有点大,慎用(建议高级查询的时候用)。
- FullPageModel:查询模型,对比PageModel多了查询集合QueryItems和过滤FilterFields。慎用(原因同上)。
2.属性名约束
- 编号查询:编号查询最好是以Id结尾,不然如果编号为字符串的话,查询方式会以Contains形式查询。
- 时间格式:以 Start,End 结尾 ,生成条件为 >= 和<=。
- 数字范围:属性名称 以 Min,Max 结尾 ,生成条件为 >= 和<=。
- 字符串查询: 名字需要和表字段一致,生成条件为 Contains
3.特性约束。这部分主要为了弥补属性名的不足。可以使用特性ConditionAttribute。下面给出参数介绍以及使用例子。
- 属性名:PropertyName
- 属性值:Value
- 查询方法:Condition
- 查询类型: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 = true, bool isClearReserved = true)。参数解析:属性名、降序升序、是否清掉之前的排序
- 默认排序方法 AddOrderBy(string propName, bool isDesc = true)。参数解析:属性名、降序升序。可添加多个,优先级按照,添加的顺序来。
/// <summary>
/// 设置默认的排序条件,并清空之前保留的排序条件(如果isClearReserved为true)。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="propName">要排序的属性名。</param>
/// <param name="isDesc">排序方向,true为降序,false为升序。</param>
/// <param name="isClearReserved">是否清空之前保留的排序条件。</param>
/// <returns>返回修改后的QueryModel实例。</returns>
public static QueryModel DefaultOrderBy(this QueryModel input, string propName, bool isDesc, bool isClearReserved)
{
// 方法实现
}
/// <summary>
/// 向QueryModel中添加一个排序条件。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="propName">要排序的属性名。</param>
/// <param name="isDesc">排序方向,true为降序,false为升序。</param>
/// <returns>返回修改后的QueryModel实例。</returns>
public static QueryModel AddOrderBy(this QueryModel input, string propName, bool isDesc)
{
// 方法实现
}
/// <summary>
/// 向QueryModel中添加两个排序条件。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="propName">第一个要排序的属性名。</param>
/// <param name="isDesc">第一个排序的方向,true为降序,false为升序。</param>
/// <param name="propName2">第二个要排序的属性名。</param>
/// <param name="isDesc2">第二个排序的方向,true为降序,false为升序。</param>
/// <returns>返回修改后的QueryModel实例。</returns>
public static QueryModel AddOrderBy(this QueryModel input, string propName, bool isDesc, string propName2, bool isDesc2)
{
// 方法实现
}
/// <summary>
/// 向QueryModel中添加三个排序条件。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="propName">第一个要排序的属性名。</param>
/// <param name="isDesc">第一个排序的方向,true为降序,false为升序。</param>
/// <param name="propName2">第二个要排序的属性名。</param>
/// <param name="isDesc2">第二个排序的方向,true为降序,false为升序。</param>
/// <param name="propName3">第三个要排序的属性名。</param>
/// <param name="isDesc3">第三个排序的方向,true为降序,false为升序。</param>
/// <returns>返回修改后的QueryModel实例。</returns>
public static QueryModel AddOrderBy(this QueryModel input, string propName, bool isDesc, string propName2, bool isDesc2, string propName3, bool isDesc3)
{
// 方法实现
}
/// <summary>
/// 向QueryModel实例中添加一组排序条件。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="orderByItems">包含排序信息的OrderByItem列表。</param>
/// <returns>返回修改后的QueryModel实例(在空实现中,实际上只是返回传入的实例)。</returns>
public static QueryModel AddOrderBy(this QueryModel input, List<OrderByItem> orderByItems)
{
// 方法实现
}
/// <summary>
/// 向QueryModel实例中添加多个排序条件,所有条件使用相同的排序方向。
/// </summary>
/// <param name="input">当前的QueryModel实例。</param>
/// <param name="isDesc">排序方向,true为降序,false为升序。</param>
/// <param name="propNames">要排序的属性名数组。</param>
/// <returns>返回修改后的QueryModel实例(在空实现中,实际上只是返回传入的实例)。</returns>
public static QueryModel AddMultipleOrderBy(this QueryModel input, bool isDesc, params string[] propNames)
{
// 方法实现
}
5.分组查询
- 使用特性GroupAttribute。使用分组序号,如Group(1)、如Group(1,EnumConditionType.Or)。
[Group(1)]
public string Name { get; set; }
[Group(1, EnumConditionType.Or)]
public string UserName { get; set; }
6.时间跨度查询约束
使用特性DurationAttribute。在时间的两个属性之间中的一个打上这个特性即可。如时间跨度三个月的。
/// <summary> /// 创建时间 开始 /// </summary> [Duration(3, EnumTimeType.Month)] public DateTime? CreateTimeStart { get; set; } /// <summary> /// 创建时间 结束 /// </summary> public DateTime? CreateTimeEnd { get; set; }
7.分页配置
- 没啥好说的:PageIndex 当前页 ,PageSize 显示数。
8.参数忽略
- 提供了特性NotQueryAttribute,在对于属性添加即可忽略构建查询。
9.构建表达式
- 只要类继承了QueryModel等这四个模型,即可以通过ToExpression<TSource>(),构建对于自己模型的表达式。
10.查询扩展
- 常规扩展,根据传入的4大模型过滤。例如
/// <summary>
/// 查询扩展
/// </summary>
public static IQueryable<T> WhereExt<T>(this IQueryable<T> source, QueryModel input)
{
//
}
/// <summary>
/// 查询扩展,并排序
/// </summary>
public static IQueryable<T> WhereExt<T>(this IQueryable<T> source, Expression<Func<T, bool>> expression, params OrderByItem[] orderByItems)
{
//
}
- 部分查询扩展,可返回传入的模型类(如果属性匹配得上的话)
/// <summary>
/// Select扩展方法
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TTarget"></typeparam>
/// <param name="query"></param>
/// <returns></returns>
public static IQueryable<TTarget> Select<TSource, TTarget>(this IQueryable<TSource> query)
{
}
- IQueryable扩展方法
/// <summary>
/// 根据QueryModel中的条件过滤IQueryable<T>并返回结果列表。
/// </summary>
/// <param name="source">待处理的IQueryable<T>。</param>
/// <param name="input">包含过滤条件的QueryModel。</param>
/// <returns>未经过滤的T类型列表。</returns>
public static List<T> ToList<T>(this IQueryable<T> source, QueryModel input)
{
// 实现
}
/// <summary>
/// 根据FullQueryModel中的复杂条件过滤IQueryable<T>并返回结果列表。
/// </summary>
/// <param name="source">待处理的IQueryable<T>。</param>
/// <param name="input">包含复杂过滤条件的FullQueryModel。</param>
/// <returns>未经过滤的T类型列表。</returns>
public static List<T> ToList<T>(this IQueryable<T> source, FullQueryModel input)
{
// 实现
}
/// <summary>
/// 根据PageModel中的分页参数对IQueryable<T>进行分页处理,并返回包含数据列表和总记录数的元组。
/// </summary>
/// <param name="query">待分页处理的IQueryable<T>。</param>
/// <param name="input">包含分页参数的PageModel。</param>
/// <returns>包含未分页数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) ToPageList<T>(this IQueryable<T> query, PageModel input)
{
// 实现
}
/// <summary>
/// 根据FullPageModel中的分页和过滤条件对IQueryable<T>进行处理,并返回包含数据列表和总记录数的元组
/// </summary>
/// <param name="query">待处理的IQueryable<T>。</param>
/// <param name="input">包含分页和过滤条件的FullPageModel。</param>
/// <returns>包含未分页且未过滤的数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) ToPageList<T>(this IQueryable<T> query, FullPageModel input)
{
// 实现
}
/// <summary>
/// 对IQueryable<T>进行分页处理,并返回包含数据列表和总记录数的元组。
/// </summary>
/// <param name="query">待分页处理的IQueryable<T>。</param>
/// <param name="input">包含分页参数的PageModel。</param>
/// <returns>包含未分页数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) Page<T>(this IQueryable<T> query, PageModel input)
{
// 实现
}
/// <summary>
/// 根据FullPageModel中的分页和过滤条件对IQueryable<T>进行处理,并返回包含数据列表和总记录数的元组。
/// </summary>
/// <param name="query">待处理的IQueryable<T>。</param>
/// <param name="input">包含分页和过滤条件的FullPageModel。</param>
/// <returns>包含未分页且未过滤的数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) Page<T>(this IQueryable<T> query, FullPageModel input)
{
// 实现
}
/// <summary>
/// 根据PageModel中的分页和排序参数对IQueryable<T>进行分页和排序处理,并返回包含排序后数据列表和总记录数的元组。
/// </summary>
/// <param name="query">待分页和排序处理的IQueryable<T>。</param>
/// <param name="input">包含分页和排序参数的PageModel。</param>
/// <returns>包含未分页且未排序的数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) OrderPageList<T>(this IQueryable<T> query, PageModel input)
{
// 实现
}
/// <summary>
/// 根据FullPageModel中的分页、排序和过滤条件对IQueryable<T>进行处理,并返回包含排序后数据列表和总记录数的元组。
/// </summary>
/// <param name="query">待处理的IQueryable<T>。</param>
/// <param name="input">包含分页、排序和过滤条件的FullPageModel。</param>
/// <returns>包含未分页、未排序且未过滤的数据列表和未计算总记录数的元组。</returns>
public static (List<T> list, int total) OrderPageList<T>(this IQueryable<T> query, FullPageModel input)
{
// 实现
}
- IQueryable扩展方法之排序
/// <summary>
/// 根据单个属性名对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="propName">要排序的属性名。</param>
/// <param name="isDesc">指示排序是否为降序。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, string propName, bool isDesc)
{
// 实现
}
/// <summary>
/// 根据两个属性名对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="propName">第一个要排序的属性名。</param>
/// <param name="isDesc">第一个排序是否为降序。</param>
/// <param name="propName2">第二个要排序的属性名。</param>
/// <param name="isDesc2">第二个排序是否为降序。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, string propName, bool isDesc, string propName2, bool isDesc2)
{
// 实现
}
/// <summary>
/// 根据三个属性名对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="propName">第一个要排序的属性名。</param>
/// <param name="isDesc">第一个排序是否为降序。</param>
/// <param name="propName2">第二个要排序的属性名。</param>
/// <param name="isDesc2">第二个排序是否为降序。</param>
/// <param name="propName3">第三个要排序的属性名。</param>
/// <param name="isDesc3">第三个排序是否为降序。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, string propName, bool isDesc, string propName2, bool isDesc2, string propName3, bool isDesc3)
{
// 实现
}
/// <summary>
/// 根据多个属性名对IQueryable<TEntity>进行排序。属性名和排序方向通过数组提供。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="isDesc">是否所有排序都应为降序,此参数在可变参数场景下可能不被直接使用,而是作为方法重载的标识。</param>
/// <param name="propNames">要排序的属性名数组。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, bool isDesc, params string[] propNames)
{
// 实现
}
/// <summary>
/// 根据IQueryModel中的排序信息对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="queryModel">包含排序信息的IQueryModel。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, IQueryModel queryModel)
{
// 实现
}
/// <summary>
/// 根据OrderByItem数组中的排序项对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="orderByItems">包含排序属性和排序方向的OrderByItem数组。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, params OrderByItem[] orderByItems)
{
// 实现
}
/// <summary>
/// 根据OrderByItem列表中的排序项对IQueryable<TEntity>进行排序。
/// </summary>
/// <param name="input">待排序的IQueryable<TEntity>。</param>
/// <param name="orderByItems">包含排序属性和排序方向的OrderByItem列表。</param>
/// <returns>未应用排序的IQueryable<TEntity>。</returns>
public static IQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> input, List<OrderByItem> orderByItems)
{
// 实现
}
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. |
.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.