Expression拼接条件,Expression.And

2024-06-10 10:18
文章标签 expression 拼接 条件

本文主要是介绍Expression拼接条件,Expression.And,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;namespace WuZiFenGongSiInfomation.Common
{/// <summary>/// Lambda表达式拼接扩展类/// </summary>/// https://blogs.msdn.microsoft.com/meek/2008/05/02/linq-to-entities-combining-predicates//// http://www.albahari.com/nutshell/predicatebuilder.aspxpublic static class ExpressionUtility{/// <summary>/// Lambda表达式拼接/// </summary>/// <typeparam name="T"></typeparam>/// <param name="first"></param>/// <param name="second"></param>/// <param name="merge"></param>/// <returns></returns>public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge){// build parameter map (from parameters of second to parameters of first)var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);// replace parameters in the second lambda expression with parameters from the firstvar secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);// apply composition of lambda expression bodies to parameters from the first expression return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);}/// <summary>/// and扩展/// </summary>/// <typeparam name="T"></typeparam>/// <param name="first"></param>/// <param name="second"></param>/// <returns></returns>public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second){return first.Compose(second, Expression.And);}/// <summary>/// or扩展/// </summary>/// <typeparam name="T"></typeparam>/// <param name="first"></param>/// <param name="second"></param>/// <returns></returns>public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second){return first.Compose(second, Expression.Or);}}/// <summary>/// /// </summary>public class ParameterRebinder : ExpressionVisitor{private readonly Dictionary<ParameterExpression, ParameterExpression> map;/// <summary>/// /// </summary>/// <param name="map"></param>public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map){this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();}/// <summary>/// /// </summary>/// <param name="map"></param>/// <param name="exp"></param>/// <returns></returns>public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp){return new ParameterRebinder(map).Visit(exp);}/// <summary>/// /// </summary>/// <param name="p"></param>/// <returns></returns>protected override Expression VisitParameter(ParameterExpression p){ParameterExpression replacement;if (map.TryGetValue(p, out replacement)){p = replacement;}return base.VisitParameter(p);}}
}

初始化Expression<Func<T, bool>>,参考代码:

Expression<Func<ArticleWhere, bool>> express= g=>true;

//拼接条件

 if (!string.IsNullOrEmpty(where?.ChannelId))
 {
        express = express.And(x => x.ChannelId == where.ChannelId);
 }

 

转载自:

https://blog.csdn.net/shujudeliu/article/details/84314642

这篇关于Expression拼接条件,Expression.And的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1047886

相关文章

SQL Server中,添加数据库到AlwaysOn高可用性组条件

1、将数据添加到AlwaysOn高可用性组,需要满足以下条件: 2、更多具体AlwaysOn设置,参考:https://msdn.microsoft.com/zh-cn/library/windows/apps/ff878487(v=sql.120).aspx 注:上述资源来自MSDN。

线程间通信方式(互斥(互斥锁)与同步(无名信号量、条件变量))

1通信机制:互斥与同步 线程的互斥通过线程的互斥锁完成; 线程的同步通过无名信号量或者条件变量完成。 2  互斥 2.1 何为互斥?         互斥是在多个线程在访问同一个全局变量的时候,先让这个线程争抢锁的资源,那个线程争抢到资源,它可以访问这个变量,没有争抢到资源的线程不能够访问这个变量。那这种只有一个线程能够访问到这个变量的现象称之为线程间互斥。 2.2互斥锁API 1.

力扣SQL50 销售分析III having + 条件计数

Problem: 1084. 销售分析III 👨‍🏫 参考题解 Code select s.product_id,p.product_namefrom sales s left join product pon s.product_id = p.product_idgroup by product_idhaving count(if(sale_date between

yii2数据条件查询-where专题

yii2数据条件查询-where专题 条件查询 c u s t o m e r s = C u s t o m e r : : f i n d ( ) − > w h e r e ( customers = Customer::find()->where( customers=Customer::find()−>where(cond)->all(); $cond就是我们所谓的条件,条件的写法也根

编程参考 - GCC的条件编译

4 Conditionals 条件是一种指令,它指示预处理器选择是否在传递给编译器的最终标记流中包含一段代码。预处理器条件可以测试算术表达式,也可以测试名称是否定义为宏,或者使用特殊的defined操作符同时测试这两种表达式。 A conditional is a directive that instructs the preprocessor to select whether o

Race Condition竞争条件

Race Condition Question – why was there no race condition in the first solution (where at most N – 1) buffers can be filled?Processes P0 and P1 are creating child processes using the fork() system

力扣SQL50 每月交易 I 求和 SUM(条件表达式) DATE_FORMAT(日期,指定日期格式)

Problem: 1193. 每月交易 I 👨‍🏫 参考题解 Code select DATE_FORMAT(trans_date, '%Y-%m') AS month,country,count(*) as trans_count,count(if(state = 'approved', 1, NULL)) as approved_count,sum(amount) as

前端实现列表多条件查询/搜索功能

在前端开发中,实现一个多条件查询功能是常见的需求,尤其是在处理表格数据时。下面我将通过一个简单的Vue组件示例,来展示如何实现一个多条件查询功能。 组件数据结构 首先,定义组件的数据结构,包括查询条件和过滤后的表格数据: data() {return {// 列表搜索条件cseAttributeData: {parcelCode: "",landType: "",isIndexesCover

张量的拼接和numpy拼接区别

拼接式张量的下扩展,表现为两个张量尺寸的和 框架不同: NumPy 是 Python 科学计算的基础库,主要用于处理多维数组和矩阵。PyTorch 是深度学习框架,主要用于处理张量,支持 GPU 加速。 函数名称和参数: NumPy 使用 concatenate 和 stack 函数。PyTorch 使用 cat 和 stack 函数。 维度参数: NumPy 使用 axis 参数指定沿哪个维

C/C++打假:条件分支语句switch..case效率比if..else高?

很久很久以前,有人教导我说条件分支大于4条时,switch..case效率会比if..else高,条件分支为10条时,switch..case效率会比if..else快一倍不止。随着条件分支越多,效率差异越大。今日得闲,我做了个测试来验证这条经验的真假。以下分3个case对比这两种条件选择分支的性能,每个case重复两遍以上确保测试数据稳定。测试所用系统为Ubuntu 22.04