当前位置:网站首页>Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
2022-04-23 13:59:00 【流苏1990】
//处理多个属性生成Expression表达式,从Expression<Func<TSource, object>> 到Expression<Func<TSource, object>>[],单个到多个
//从 x=>new{x.a,x.b} 到 x=>x.a x=>x.b
private static Expression<Func<TSource, object>>[] ProcessExpressionObjet<TSource>(Expression<Func<TSource, object>> propertyExpression)
{
Expression<Func<TSource, object>>[] expressions = new Expression<Func<TSource, object>>[] { };
Expression expression = propertyExpression.Body;
while (expression.NodeType == ExpressionType.Convert || expression.NodeType == ExpressionType.ConvertChecked)
expression = ((UnaryExpression)expression).Operand;
//Parameter
ParameterExpression parameterExpression = propertyExpression.Parameters.Single<ParameterExpression>();
if (expression is NewExpression)
{
NewExpression newExpression = expression as NewExpression;
if (newExpression == null)
return expressions;
expressions = new Expression<Func<TSource, object>>[newExpression.Arguments.Count];
int index = 0;
foreach (Expression subExpression in newExpression.Arguments)
{
MemberExpression member = subExpression as MemberExpression;
Expression exProperty = Expression.Property(parameterExpression, member.Member.Name);
var body = Expression.Convert(exProperty, typeof(object));
expressions[index] = Expression.Lambda<Func<TSource, object>>(body, parameterExpression);
index++;
}
return expressions;
}
else if (expression is MemberExpression)
{
expressions = new Expression<Func<TSource, object>>[1];
Expression exProperty = Expression.Property(parameterExpression, (expression as MemberExpression).Member.Name);
var body = Expression.Convert(exProperty, typeof(object));
expressions[0] = Expression.Lambda<Func<TSource, object>>(body, parameterExpression);
return expressions;
}
else
return expressions;
}
来源思路:
获取实体T的所有属性的lambda表达式数组:如x->x.a,x->x.b,x->x.b,x->x.c
public static Expression<Func<T, object>>[] GetExpressions<T>()
{
var properties = typeof(T).GetProperties();
Expression<Func<T, object>>[] expressions = new Expression<Func<T, object>>[properties.Length];
var p = Expression.Parameter(typeof(T), "x");
for (int i = 0; i < properties.Length; i++)
{
Expression exProperty = Expression.Property(p, properties[i]);
var body = Expression.Convert(exProperty, typeof(object));
expressions[i] = Expression.Lambda<Func<T, object>>(body, p);
}
return expressions;
}
版权声明
本文为[流苏1990]所创,转载请带上原文链接,感谢
https://blog.csdn.net/fuweiping/article/details/107232789
边栏推荐
- JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
- Yarn online dynamic resource tuning
- SQL learning window function
- Dynamic subset division problem
- Influence of openssh version on SSH mutual trust creation in RAC environment
- 函数只执行第一次的执行一次 once函数
- Oracle RAC database instance startup exception analysis IPC send timeout
- JS 烧脑面试题大赏
- UML Unified Modeling Language
- Strange bug of cnpm
猜你喜欢

Port occupied 1

Reading notes: meta matrix factorization for federated rating predictions

Express ② (routage)

JS 烧脑面试题大赏

Modify the Jupiter notebook style

解决方案架构师的小锦囊 - 架构图的 5 种类型

Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)

The art of automation

程序编译调试学习记录

Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
随机推荐
Kettle--控件解析
Chapter 15 new technologies of software engineering
收藏博客贴
Express中间件③(自定义中间件)
scikit-learn構建模型的萬能模板
Analysis of cluster component gpnp failed to start successfully in RAC environment
About note 1
STM32学习记录0007——新建工程(基于寄存器版)
OSS cloud storage management practice (polite experience)
Tensorflow & pytorch common error reporting
村上春树 --《当我谈跑步时,我谈些什么》句子摘录
JS 烧脑面试题大赏
基础知识学习记录
专题测试05·二重积分【李艳芳全程班】
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
力扣刷题 101. 对称二叉树
crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
Choreographer full resolution
VsCode-Go
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration