1.准备数据实体

        public class Data
{
public string AccountNO { get; set; } public int Count { get; set; }
}

创建测试数据

 public static List<Data> GetTestData()
{
Data account = new Data
{
AccountNO = "",
Count =
}; Data account1 = new Data
{
AccountNO = "",
Count =
}; Data account2 = new Data
{
AccountNO = "",
Count =
}; Data account3 = new Data
{
AccountNO = "",
Count =
}; Data account4 = new Data
{
AccountNO = "",
Count =
};
var rel = new List<Data>
{
account,
account1,
account2,
account3,
account4
};
return rel;
}

2.准备查询条件实体

        public class Condition
{
[DynamicExpression(Name = "AccountNO", Operator = "Contains")]
public string Name { get; set; } [DynamicExpression(Name = "Count", Operator = ">")]
public int? Age { get; set; }
}

数据实体2个字段一个帐号一个年龄大小

查询条件2个字段一个姓名(Name帐号)一个年龄(Age)

为了确保查询字段和数据字段名字保持一直 引用一个自定义特性 在查询条件的每个字段上面标记

自定义特性包含2个字段一个Name 需要跟数据实体保持一致的名称(注:必须和数据实体的字段对应) Operator 表示运算逻辑符

附自定义实体代码

 [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
public class DynamicExpressionAttribute : Attribute
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } /// <summary>
/// 运行符号
/// </summary>
public string Operator { get; set; }
}

从查询条件中提取特效值的扩展类

    public static class CustomAttributeExtension<TAttribute>
where TAttribute : Attribute
{
/// <summary>
/// Cache Data
/// </summary>
private static readonly Dictionary<string, TAttribute> Cache = new Dictionary<string, TAttribute>(); /// <summary>
/// 获取CustomAttribute Value
/// </summary>
/// <param name="type">实体对象类型</param>
/// <param name="propertyInfo">实体对象属性信息</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
public static TAttribute GetCustomAttributeValue(Type type, PropertyInfo propertyInfo)
{
var key = BuildKey(type, propertyInfo);
if (!Cache.ContainsKey(key))
{
CacheAttributeValue(type, propertyInfo);
}
return Cache[key];
} /// <summary>
/// 获取CustomAttribute Value
/// </summary>
/// <param name="sourceType">实体对象数据类型</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
public static TAttribute GetCustomAttributeValue(Type sourceType)
{
var key = BuildKey(sourceType, null);
if (!Cache.ContainsKey(key))
{
CacheAttributeValue(sourceType, null);
}
return Cache[key];
} /// <summary>
/// 获取实体类上的特性
/// </summary>
/// <param name="type">实体对象类型</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
private static TAttribute GetClassAttributes(Type type)
{
var attribute = type.GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault();
return (TAttribute)attribute;
} /// <summary>
/// 获取实体属性上的特性
/// </summary>
/// <param name="propertyInfo">属性信息</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
private static TAttribute GetPropertyAttributes(PropertyInfo propertyInfo)
{
var attribute = propertyInfo?.GetCustomAttributes(typeof(TAttribute), false).FirstOrDefault();
return (TAttribute)attribute;
} /// <summary>
/// 缓存Attribute Value
/// <param name="type">实体对象类型</param>
/// <param name="propertyInfo">实体对象属性信息</param>
/// </summary>
private static void CacheAttributeValue(Type type, PropertyInfo propertyInfo)
{
var key = BuildKey(type, propertyInfo);
TAttribute value;
if (propertyInfo == null)
{
value = GetClassAttributes(type);
}
else
{
value = GetPropertyAttributes(propertyInfo);
} lock (key + "_attributeValueLockKey")
{
if (!Cache.ContainsKey(key))
{
Cache[key] = value;
}
}
} /// <summary>
/// 缓存Collection Name Key
/// <param name="type">type</param>
/// <param name="propertyInfo">propertyInfo</param>
/// </summary>
private static string BuildKey(Type type, PropertyInfo propertyInfo)
{
if (string.IsNullOrEmpty(propertyInfo?.Name))
{
return type.FullName;
}
return type.FullName + "." + propertyInfo.Name;
}
}

根据数据和查询条件生成表达式

 public static Func<TResult, bool> GetDynamicExpression<TResult, TCondition>(this IEnumerable<TResult> data, TCondition condtion)
where TResult : class
where TCondition : class
{
Type tConditionType = typeof(TCondition);
Type tResultType = typeof(TResult); Expression totalExpr = Expression.Constant(true);
ParameterExpression param = Expression.Parameter(typeof(TResult), "n");
foreach (PropertyInfo property in tConditionType.GetProperties())
{
string key = property.Name;
object value = property.GetValue(condtion);
if (value != null && value.ToString() != string.Empty)
{
DynamicExpressionAttribute dynamicExpressionAttribute = CustomAttributeExtension<DynamicExpressionAttribute>.GetCustomAttributeValue(tConditionType, property); //等式左边的值
string name = dynamicExpressionAttribute.Name ?? key;
Expression left = Expression.Property(param, tResultType.GetProperty(name));
//等式右边的值
Expression right = Expression.Constant(value); Expression filter;
switch (dynamicExpressionAttribute.Operator)
{
case "!=":
filter = Expression.NotEqual(left, right);
break;
case ">":
filter = Expression.GreaterThan(left, right);
break;
case ">=":
filter = Expression.GreaterThanOrEqual(left, right);
break;
case "<":
filter = Expression.LessThan(left, right);
break;
case "<=":
filter = Expression.LessThanOrEqual(left, right);
break;
case "Contains":
filter = Expression.Call(Expression.Property(param, tResultType.GetProperty(name)), typeof(string).GetMethod("Contains", new [] { typeof(string) }), Expression.Constant(value)); ;
break;
default:
filter = Expression.Equal(left, right);
break;
}
totalExpr = Expression.And(filter, totalExpr);
}
}
var predicate = Expression.Lambda(totalExpr, param);
var dynamic = (Func<TResult, bool>)predicate.Compile();
return dynamic;
}

测试 : (查询帐号包含1的数据)

 public static void Test()
{
var data = GetTestData();
var codition = new Condition { Name = "1"};
var dynamicExpression = GetDynamicExpression(data, codition);
var query1 = data.Where(dynamicExpression);
}

动态LINQ(Lambda表达式)的更多相关文章

  1. 【转】EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句

    传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...

  2. 动态创建Lambda表达式实现高级查询

    需求简介 最近这几天做的东西总算是回归咱的老本行了,给投资管理项目做一个台账的东西,就是类似我们的报表.其 中有一个功能是一个高级查询的需求,在查询条件方面大概有7.8个查询条件.需求就是如果一个条件 ...

  3. 动态组合lambda 表达式

    //记录实体集合—动态组合lambda 表达式 Expression<Func<AdEntity, bool>> thirdWhere = p => p.Observer ...

  4. easyui datagrid remoteSort的实现 Controllers编写动态的Lambda表达式 IQueryable OrderBy扩展

    EF 结合easy-ui datagrid 实现页面端排序 EF动态编写排序Lambda表达式 1.前端页面 var mainListHeight = $(window).height() - 20; ...

  5. 动态构建Lambda表达式实现EF动态查询

    在使用Entity Framework做数据查询的时候,查询条件往往不是固定的,需要动态查询.可以通过动态构建Lamda表达式来实现动态查询. Lamda表达式 使用Lamda表达式可以很方便的按条件 ...

  6. 动态创建 Lambda 表达式

    首先我们看一个简单 Lambda 表达式的构成. i => i > 5 在这个表达式中,"i" 被称为 Parameter,"i > 5" 是 ...

  7. LinQ—Lambda表达式

    概述 本篇博客主要解说lambda表达式,在这里将它的来龙去脉,主要是从托付,匿名函数这些方面过度讲的,当然,在讲托付和匿名函数的时候,主要是从Lambda的角度出发讲的,可能它们还具有其他的一些作用 ...

  8. 动态拼接lambda表达式树

    前言 最近在优化同事写的代码(我们的框架用的是dapperLambda),其中有一个这样很普通的场景——界面上提供了一些查询条件框供用户来进行过滤数据.由于dapperLambda按条件查询时是传入表 ...

  9. [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)

    声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...

  10. 使用Expression动态创建lambda表达式

    using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...

随机推荐

  1. 用WPE+CCproxy+自动代理截取安卓游戏封包

    wpe三件套:https://pan.baidu.com/s/19gI2GPZ0iuu4wpKljCOn4A 用WPE+CCproxy+自动代理截取安卓游戏封包>>

  2. 【web前端】移动端控制台插件,手机端页面查看相关页面控制台信息

    一般调试手机端页面时,基本是在PC端使用手机模式进行断点或console调试.或查看有用的控制台信息的,但依旧有部分功能无法在PC端调试,经常需要使用alert进行断点,然后在手机端看效果,但是这样并 ...

  3. Prometheus监控学习笔记之Prometheus存储

    0x00 概述 Prometheus之于kubernetes(监控领域),如kubernetes之于容器编排.随着heapster不再开发和维护以及influxdb 集群方案不再开源,heapster ...

  4. Python3 hasattr()、getattr()、setattr()函数简介

    Python3 hasattr().getattr().setattr()函数简介 一.hasattr(object, name) 判断object对象中是否存在name属性,当然对于python的对 ...

  5. c++实现“扫描检测硬件改动”

    这里需要用到cfgmgr32.h,参考了网上好几篇博文. #include <windows.h> #include <stdio.h> #include <cfgmgr ...

  6. ORA-38301:can not perform DDL/DML Over Object in Recycle Bin 11.2.0.4

    我们最近有两台测试服务器在oci direct load期间出现下列异常: 从表象上看,是我们在对表执行ddl操作,确实内部也是用了truncate table XXX,可是这个XXX并不是回收站里面 ...

  7. Caused by: com.rabbitmq.client.ShutdownSignalException: connection error

    周五下午的时候升级了一个环境,跑了批处理sh升级脚本后,启动时报下列错误: INFO | jvm 1 | 2017/02/24 17:39:09 | java.io.IOException INFO ...

  8. Java程序员必备的Intellij插件(长期更新,截止到2018-05-03)

    善用Intellij插件可大幅提升我们的效率 以下是我用过不错的Intellij插件 1. .ignore 生成各种ignore文件,一键创建git ignore文件的模板,免得自己去写 截图:   ...

  9. GDPR

    http://column.caijing.com.cn/20180523/4457753.shtml

  10. 列表与if语句的结合

    # 1.判断一个数是否是水仙花数, 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. \ # 那这个数就是一个水仙花数, 例如: 153 = 1**3 + 5**3 + 3**3 # ...