C#之Expression表达式树实例
作者:shichen2014 时间:2023-02-24 20:54:31
本文实例讲述了C#之Expression表达式树,分享给大家供大家参考。具体实现方法如下:
表达式树表示树状数据结构的代码,树状结构中的每个节点都是一个表达式,例如一个方法调用或类似 x < y 的二元运算
1.利用 Lambda 表达式创建表达式树
Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
2.编译表达式树,该方法将表达式树表示的代码编译成一个可执行委托
expr.Compile()(1, 2, 3)
3.IQueryable<T>的扩展方法,WhereIn的实现
var d = list.AsQueryable().WhereIn(o => o.Id1, new int[] { 1, 2 });
完整代码如下:
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
//使用LambdaExpression构建表达式树
Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
Console.WriteLine(expr.Compile()(1, 2, 3));
//使用LambdaExpression构建可执行的代码
Func<int, int, int, int> fun = (x, y, z) => (x + y) / z;
Console.WriteLine(fun(1, 2, 3));
//动态构建表达式树
ParameterExpression pe1 = Expression.Parameter(typeof(int), "x");
ParameterExpression pe2 = Expression.Parameter(typeof(int), "y");
ParameterExpression pe3 = Expression.Parameter(typeof(int), "z");
var body = Expression.Divide(Expression.Add(pe1, pe2), pe3);
var w = Expression.Lambda<Func<int, int, int, int>>(body, new ParameterExpression[] { pe1, pe2, pe3 });
Console.WriteLine(w.Compile()(1, 2, 3));
List<Entity> list = new List<Entity> { new Entity { Id1 = 1 }, new Entity { Id1 = 2 }, new Entity { Id1 = 3 } };
var d = list.AsQueryable().WhereIn(o => o.Id1, new int[] { 1, 2 });
d.ToList().ForEach(o =>
{
Console.WriteLine(o.Id1);
});
Console.ReadKey();
}
}
public class Entity
{
public ObjectId Id;
public int Id1;
public string Name { get; set; }
}
public static class cc
{
public static IQueryable<T> WhereIn<T, TValue>(this IQueryable<T> query, Expression<Func<T, TValue>> obj, IEnumerable<TValue> values)
{
return query.Where(BuildContainsExpression(obj, values));
}
private static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector)
{
throw new ArgumentNullException("valueSelector");
}
if (null == values)
{
throw new ArgumentNullException("values");
}
var p = valueSelector.Parameters.Single();
if (!values.Any()) return e => false;
var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate(Expression.Or);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#套接字(Socket)通信之UDP组播详解
Java编程实现帕斯卡三角形代码示例
![](https://img.aspxhome.com/file/2023/3/58303_0s.gif)
Android自定义控制条效果
![](https://img.aspxhome.com/file/2023/1/106051_0s.jpg)
浅析java中的取整(/)和求余(%)
Java 设计模式中的策略模式详情
![](https://img.aspxhome.com/file/2023/1/57731_0s.png)
RocketMQ producer同步发送单向发送源码解析
在c#中使用servicestackredis操作redis的实例代码
C语言 奇偶排序算法详解及实例代码
![](https://img.aspxhome.com/file/2023/6/114926_0s.gif)
C#实现简单的计算器功能
![](https://img.aspxhome.com/file/2023/8/70408_0s.jpg)
java设计模式(实战)-责任链模式
![](https://img.aspxhome.com/file/2023/3/132063_0s.png)
深入理解C#管道式编程
![](https://img.aspxhome.com/file/2023/3/96383_0s.png)
SpringBoot之bootstrap和application的区别解读
![](https://img.aspxhome.com/file/2023/5/106145_0s.png)
解析c#操作excel后关闭excel.exe的方法
详解Java中的时区类TimeZone的用法
ajaxFileUpload插件,C#返回Json数据报错问题的解决方案
![](https://img.aspxhome.com/file/2023/0/91360_0s.png)
java 中二分法查找的应用实例
C# DateTime.Compare()方法案例详解
C#图像处理之图像均值方差计算的方法
java8实现List中对象属性的去重方法
详解SpringBoot缓存的实例代码(EhCache 2.x 篇)
![](https://img.aspxhome.com/file/2023/1/77931_0s.jpg)