当前位置:网站首页>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
边栏推荐
- Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
- Chapter 15 new technologies of software engineering
- Move blog to CSDN
- Strange bug of cnpm
- AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
- Port occupied 1
- Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
- scikit-learn构建模型的万能模板
- 商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
- Introduction to spark basic operation
猜你喜欢

Leetcode | 38 appearance array

记录一个奇怪的bug:缓存组件跳转之后出现组件复制

The latest development of fed digital currency

JMeter pressure test tool

【vmware】vmware tools 地址

第一章 电商秒杀商品回顾

33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?

UML统一建模语言

基于Ocelot的gRpc网关

Using Baidu Intelligent Cloud face detection interface to achieve photo quality detection
随机推荐
Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
JS 烧脑面试题大赏
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
Kettle--控件解析
联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作
[code analysis (5)] communication efficient learning of deep networks from decentralized data
AtomicIntegerArray源码分析与感悟
10g database cannot be started when using large memory host
神经元与神经网络
MySQL index [data structure + index creation principle]
专题测试05·二重积分【李艳芳全程班】
scikit-learn构建模型的万能模板
蓝绿发布、滚动发布、灰度发布,有什么区别?
基础知识学习记录
MySQL [SQL performance analysis + SQL tuning]
Es introduction learning notes
Express中间件③(自定义中间件)
程序编译调试学习记录
Analysis of cluster component gpnp failed to start successfully in RAC environment