Skip to content

谓词筛选表达式Expression<Func<T, bool>>的扩展库

License

Notifications You must be signed in to change notification settings

xljiulang/PredicateLib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PredicateLib

PredicateLib是谓词筛选表达式Expression<Func<T, bool>>的一个扩展库,它可以帮你创建一个复杂且灵活的Expression<Func<T, bool>>,以作为EF、MongoDB Driver等ORM框架的查询条件。

nuget

PM> install-package PredicateLib

1 Predicate的创建

1.1 true或false Predicate

var predicate = Predicate.True<User>();

表达式输出

item => true

1.2 通过属性创建Predicate

var predicate = Predicate.Create<User>("age", 2, Operator.GreaterThan);

表达式输出

item => (item.Age > 2)

2 Predicate的逻辑扩展

var predicate = Predicate
    .True<User>()
    .And(item => item.Name == "laojiu");

if (true)
{
    predicate = predicate.And(item => item.Age > 10 && item.Age < 20);
}

表达式输出

item => ((True AndAlso (item.Name == "laojiu")) AndAlso ((item.Age > 10) AndAlso (item.Age < 20)))

3 Condition转换为Predicate

Condition对象支持传入IEnumerable<KeyValuePair<,>>IEnumerable<ConditionItem>等类型作为条件项,然后转换为Predicate,适用于前端传入查询不确定的字段与值,后端不需要修改代码的需求。

var uri = new Uri("http://www.xx.com/?age=1&name=laojiu&id=001");

var condition = uri.AsCondition<User>()
    .OperatorFor(item => item.Age, Operator.GreaterThan)
    .IgnoreFor(item => item.Id);

var predicate = condition.ToAndPredicate();

表达式输出

item => ((item.Age > 1) AndAlso item.Name.Contains("laojiu".Value))

About

谓词筛选表达式Expression<Func<T, bool>>的扩展库

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages