using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace EasyFrame.Common
{
public static class LambdaCommon
{
#region 表达式工具
/// <summary>
/// 相当于&&操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisFilter">已生成的过滤条件</param>
/// <param name="otherFilter">未生成的过滤条件</param>
/// <returns>新的过滤</returns>
public static Expression GotoAndAlso(this Expression thisFilter, Expression otherFilter)
{
return Expression.AndAlso(thisFilter, otherFilter);
}
/// <summary>
/// 相当于||操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisFilter">已生成的过滤条件</param>
/// <param name="otherFilter">未生成的过滤条件</param>
/// <returns>新的过滤</returns>
public static Expression GotoOrElse(this Expression thisFilter, Expression otherFilter)
{
return Expression.OrElse(thisFilter, otherFilter);
}
/// <summary>
/// 相当于==操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.Equal(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqual<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于小于等于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanOrEqual<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于或等于
return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于!=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoNotEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.NotEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
} /// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于>操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThan<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于
return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于小于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThan<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于
return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 字符串包含
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoContains(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.Call(Expression.Property(thisParameterExpression, propertieName), typeof(string).GetMethod("Contains"), Expression.Constant(propertieValue));
} /// <summary>
/// 相当于小于或等于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于或等于
return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 相当于>操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于
return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 相当于小于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于
return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 包含操作 相当余 a=> arr.Contains(a.ID)
/// </summary>
/// <param name="thisParameterExpression"></param>
/// <param name="propertieName"></param>
/// <param name="propertieValue"></param>
/// <returns></returns>
public static Expression ContainsOperations(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
MethodInfo method = null;
MemberExpression member = Expression.Property(thisParameterExpression, propertieName);
var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(m => m.Name == "Contains");
foreach (var m in containsMethods)
{
if (m.GetParameters().Count() == )
{
method = m;
break;
}
}
method = method.MakeGenericMethod(member.Type);
var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(propertieValue), member });
return exprContains;
} /// <summary>
/// 包含操作 相当于 a=>a.ID.Contains(key)
/// </summary>
/// <param name="thisParameterExpression"></param>
/// <param name="propertieName"></param>
/// <param name="propertieValue"></param>
/// <returns></returns>
public static Expression Contains(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
var propertyExp = Expression.Property(thisParameterExpression, propertieName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertieValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return containsMethodExp;
}
#endregion
}
}

C#_Lamada帮助类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  3. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  4. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  5. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  6. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  7. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  8. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  9. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

随机推荐

  1. 菜鸡谈OO 第一单元总结

    “OOP永远是我的好朋友爸爸!” ——来自某无能狂怒的菜鸡 身处在OO的第一个摸鱼黄金周中的我,感觉到了巨大的满足感.如果写博客这种充满意义的事情可以代替我们亲爱的作业,那么我提议每周来两个:)下面开 ...

  2. JavaScript递归函数解“汉诺塔”

    “汉诺塔”是一个著名的益智游戏.塔上有3根柱子和一套直径各不相同的空心圆盘.开始时柱子上的所有圆盘都按照从小到大的顺序堆叠.目标是通过每次移动一个圆盘到另一根柱子,最终把一堆圆盘移动到目标柱子上,过程 ...

  3. Java作业 十一(2017-11-13)

    /*关键字*/ package com.baidu.www; abstract class A { private String name; public A(String name) { this. ...

  4. 流媒体协议(一):HLS 协议

    一.HLS 概述 HLS 全称是 HTTP Live Streaming,是一个由 Apple 公司提出的基于 HTTP 的媒体流传输协议,用于实时音视频流的传输.目前HLS协议被广泛的应用于视频点播 ...

  5. Python爬虫5-利用usergent伪装访问方式

    GitHub代码练习地址:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac08_useragent.py UserAgen ...

  6. [Swift]LeetCode676. 实现一个魔法字典 | Implement Magic Dictionary

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  7. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  8. [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  9. [Swift]LeetCode815. 公交路线 | Bus Routes

    We have a list of bus routes. Each routes[i]is a bus route that the i-th bus repeats forever. For ex ...

  10. IntelliJ IDEA下SVN的配置及使用说明

    1 下载及安装SVN客户端. 到官网下载小乌龟SVN客户端,官网地址:https://tortoisesvn.net/downloads.html,根据操作系统情况选择适合版本.比如64为操作系统,如 ...