網站首頁 編程語言 正文
使用代碼
特別注意 :拼接條件時,所使用到的條件strID,strBir必須是獨立的
var predicate = PredicateBuilder.GetTrue(); predicate = predicate.And(it => it.id.ToString().Contains(strID)); predicate = predicate.And(it => it.Birthday.ToString().Contains(strBir)); predicate = predicate.And(it => it.Sex.ToString().Contains(strSex)); predicate = predicate.And(it => it.Age == 20); var lst = db.Queryable .Where(predicate).ToList();
工具類
using System; using System.Linq.Expressions; namespace WindowsFormsApp1.Tools { ? ? public static class PredicateBuilder ? ? { ? ? ? ? public static Expression> GetTrue () { return f => true; } ? ? ? ? public static Expression > GetFalse () { return f => false; } ? ? ? ? public static Expression > And (this Expression > first, Expression > second) ? ? ? ? { ? ? ? ? ? ? return first.AndAlso (second, Expression.AndAlso); ? ? ? ? } ? ? ? ? public static Expression > Or (this Expression > first, Expression > second) ? ? ? ? { ? ? ? ? ? ? return first.AndAlso (second, Expression.OrElse); ? ? ? ? } ? ? ? ? private static Expression > AndAlso (this Expression > expr1, Expression > expr2, Func func) ? ? ? ? { ? ? ? ? ? ? var parameter = Expression.Parameter(typeof(T)); ? ? ? ? ? ? var leftVisitor = new ReplaceExpressionVisitor(expr1.Parameters[0], parameter); ? ? ? ? ? ? var left = leftVisitor.Visit(expr1.Body); ? ? ? ? ? ? var rightVisitor = new ReplaceExpressionVisitor(expr2.Parameters[0], parameter); ? ? ? ? ? ? var right = rightVisitor.Visit(expr2.Body); ? ? ? ? ? ? return Expression.Lambda >( ? ? ? ? ? ? ? ? func(left, right), parameter); ? ? ? ? } ? ? ? ? private class ReplaceExpressionVisitor : ExpressionVisitor ? ? ? ? { ? ? ? ? ? ? private readonly Expression _oldValue; ? ? ? ? ? ? private readonly Expression _newValue; ? ? ? ? ? ? public ReplaceExpressionVisitor(Expression oldValue, Expression newValue) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? _oldValue = oldValue; ? ? ? ? ? ? ? ? _newValue = newValue; ? ? ? ? ? ? } ? ? ? ? ? ? public override Expression Visit(Expression node) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (node == _oldValue) ? ? ? ? ? ? ? ? ? ? return _newValue; ? ? ? ? ? ? ? ? return base.Visit(node); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
原文鏈接:https://blog.csdn.net/weixin_47492910/article/details/116020315
相關推薦
- 2024-03-08 SpringBoot項目多模塊開發詳解
- 2022-04-20 C語言函數棧幀的創建和銷毀詳解_C 語言
- 2022-09-17 詳解python中靜態方法staticmethod用法_python
- 2022-10-21 Go語言使用goroutine及通道實現并發詳解_Golang
- 2022-12-05 Flutter控制組件顯示和隱藏三種方式詳解_Android
- 2022-05-13 CLion 中文輸出亂碼
- 2023-03-27 Android數據結構優化教程_Android
- 2022-10-04 python?numpy庫中數組遍歷的方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支