# Wosperry.ExpressionExtensions
**Repository Path**: wosperry/Wosperry.ExpressionExtensions
## Basic Information
- **Project Name**: Wosperry.ExpressionExtensions
- **Description**: 使用表达式树动态构建lambda表达式,减少重复的类似代码。
- **Primary Language**: C#
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2021-10-17
- **Last Updated**: 2022-07-05
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 集合类型与参数类型举例
``` csharp
///
/// 参数类,设置为可空类型,不给值就不参与组装linq
///
public class BoxQuery
{
[WhereLarger("Width")]
public float? MinWidth { get; set; }
[WhereSmallerEqual("Width")]
public float? MaxWidth { get; set; }
[WhereSmallerEqual("Weight")]
public float? MaxWeight { get; set; }
}
```
``` csharp
///
/// Box (实体类)
///
public class Box
{
public int Id { get; set; }
public float Width { get; set; }
public float Height { get; set; }
public float Weight { get; set; }
public string Description { get; set; }
public string Remark { get; set; }
}
```
# 使用示范
为`IEnumerable`数据集提供拓展 `GetList(TParam input)`
``` csharp
// var result = boxes.Where(t => t.Width<=60 && t.Weight<=400)
List result = boxes.GetList(new BoxQuery
{
MaxWidth = 60,
MaxWeight = 400
});
```
为实现了`IQueryable`的仓储等提供拓展`BuildLambda(TParam input)`
``` csharp
// 之前
public Task> GetListByParamsAsync(BoxQuery input){
var query = _repository
.Where(input.f1 is not null, t=>t.F1==input. f1)
.Where(input.f2 is not null, t=>t.F1==input. f2)
.Where(input.f3 is not null, t=>t.F1>input. f3)
.Where(input.f4 is not null, t=>t.F1t.F1.Contains(input. f5))
.Where(input.f6 is not null, t=>t.F1==input. f6)
.Where(input.f7 is not null, t=>t.F1==input. f7)
.Where(input.f8 is not null, t=>t.F1==input. f8)
.Where(input.f9 is not null, t=>t.F1==input. f9)
.Where(input.f10 is not null, t=>t.F1==input.f10)
.Where(input.f11 is not null, t=>t.F1==input.f11);
return await AsyncExecuter.ToListAsync(query);
}
// 之后
public Task> GetListByParamsAsync(BoxQuery input){
// 是否可空由表达式目录树代码判断,使用无需写判断代码。
var query = _repository.BuildLambda(input);
return await AsyncExecuter.ToListAsync(query);
}
```
# 支持
- [x] `WhereEqualAttribute` `t=> t.Id == input.Id`
- [x] `WhereLikeAttribute` `t=>t.Name.Contains(input.Id)`
- [x] `WhereLargerAttribute` `t=>t.Age > input.Age`
- [x] `WhereLargerEqualAttribute` `t=>t.Age >= input.Age`
- [x] `WhereSmallerAttribute` `t=>t.Age < input.Age`
- [x] `WhereSmallerEqualAttribute` `t=>t.Age <= input.Age`
- [ ] `WhereInAttribute` `t=>input.Ids.Contains(t.Id)` `TODO`
- [ ] `WhereBetweenAttribute` 先使用 larger 和 smaller 两个特性共同实现,暂时不增加