EF 拉姆达 动态拼接查询语句
EF 动态拼接查询语句
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;
using System.Text; namespace Aliexpress.Common.CommonHelper
{
//public static class PredicateBuilder
//{ // /// <summary>
// /// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效
// /// </summary>
// /// <typeparam name="T"></typeparam>
// /// <returns></returns>
// public static Expression<Func<T, bool>> True<T>() { return f => true; } // /// <summary>
// /// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效
// /// </summary>
// /// <typeparam name="T"></typeparam>
// /// <returns></returns>
// public static Expression<Func<T, bool>> False<T>() { return f => false; } // public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
// Expression<Func<T, bool>> expr2)
// {
// var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
// return Expression.Lambda<Func<T, bool>>
// (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
// } // public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
// Expression<Func<T, bool>> expr2)
// {
// var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
// return Expression.Lambda<Func<T, bool>>
// (Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
// }
//} public class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map; public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
} public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
} protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;
if (map.TryGetValue(p, out replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
} public static class PredicateBuilder
{ public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
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 first
var 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);
} 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);
} 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);
}
}
}
EF 拉姆达 动态拼接查询语句的更多相关文章
- mybatis 使用记录(二) 动态拼接查询条件
2016-12-16 阅读项目代码时,在项目的xml文件中发现如下写法: SELECT student_user_id FROM tbr_student_class WHERE 1=1 <if ...
- java动态拼接sql语句并且执行时给sql语句的参数赋值
问题 在这里举一个例子,比如我要做一个多条件模糊查询,用户输入的时候有可能输入一个条件,也有可能输入两个条件,这时执行查询的sql语句就不确定了,但可以用动态拼接sql语句来解决这个问题. 解决方法 ...
- 用PredicateBuilder实现Linq动态拼接查询
在使用Linq查询的时候,特别是如果你在使用Entiry Framwork,有时会遇到动态查询的情况(客户的查询条件是不固定的拼接查询).我们能想到的第一方案应该是拼接SQL,的确这样是可以达到我们的 ...
- 获取动态SQL查询语句返回值(sp_executesql)
在写存储过程时经常会遇到需要拼接SQL语句的情况,一般情况下仅仅是为了执行拼接后的语句使用exec(@sql)即可. 而今天的一个存储过程却需要获取动态SQL的查询结果. 需求描述:在某表中根据Id值 ...
- EF动态拼接查询
1.业务中遇到个问题,需要查询如某表的id为1或者2或者3,这里是根据传递参数获取如:传递1,2或者1,3或者1,2,3这里在sql中很好拼接如下: or id= or name=3//3代表另一个字 ...
- 动态拼接SQL语句
1.参考官方文档 ? if:字符判断 ? choose (when, otherwise):分支选择 ? trim (where, set):字符串截取:其中where标签封装查询条件,set标签封装 ...
- EF 拉姆达 linq if else (整理)
首先想到: var data0 = db.T_Plants2; //这里加.AsQueryable() ) { .Where(d => d.NaturalEcosystem == true); ...
- Linq to Entity 动态拼接查询条件(重点是OR)
public static class PredicateExtensions { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效:单个OR无效 ...
- EF 拉姆达 linq 帮助类
(这个类是很早以前在网上找的,忘记出处请原谅.) 一.基本用法 [Route("List")] public ApiResult GetList(int page, int lim ...
随机推荐
- mongoexport导出数据
mongoexport用法: /***** Export MongoDB data to CSV, TSV or JSON files.options: --help ...
- Swift UI
概述 Apple近日发布了Swift编程语言,Swift是供iOS和OS X应用编程的新编程语言.相信很多开发者都在学习这门新语言. 废话不多说,下面我就来学习使用Swift创建一个简单的UI应用 ...
- C和C++的学习过程总结
总是被同学们问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复. 一家之言,欢迎拍砖哈. 1.可以考虑先学习C. 大多数时候,我们学习语言的目的,不是为了成为一个语言专家,而 ...
- 深度学习word2vec笔记之算法篇
深度学习word2vec笔记之算法篇 声明: 本文转自推酷中的一篇博文http://www.tuicool.com/articles/fmuyamf,若有错误望海涵 前言 在看word2vec的资料 ...
- FATAL:NO bootable medium found!System halted.
问题描述:致命错误,没有可引导的媒体.系统挂起.以下是在网上查的: 1:检查硬盘的类型,ide或sata接口是否在0,0或是在1,0. 2:光驱是否选择iso文件. 3:iso文件是否损坏4:virt ...
- LeetCode_Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- VS2010使用静态编译的qt库(Qt 5)
Qt 5引入了一种新的编写方式. Qt开发界面很方便,但发布程序就不那么方便了,你的把引用到的dll一起发布才行,要是能静态编译就好了,发布的时候只有一个exe多方便. 虽然以前为了方便,直接安装的q ...
- UML--一些图
通过UML来表示汽车,简洁明了. 统一建模语言--UML. 参与者Actor,参与者代表了现实世界的人.人. 用例use case,就是参与者要做什么并且获得什么.事. 业务场景,用例场景.规则. 业 ...
- LVM(1)
DM: DM: Device Mapper 逻辑设备 RAID, LVM2 DM: LVM2 快照 多路径
- [转]Laravel 4之数据库操作
Laravel 4之数据库操作 http://dingjiannan.com/2013/laravel-database/ 数据库配置 Laravel数据库配置在app/config/database ...