当前位置:网站首页>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
边栏推荐
- Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
- [code analysis (2)] communication efficient learning of deep networks from decentralized data
- About me
- Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
- Chapter 15 new technologies of software engineering
- L2-024 部落 (25 分)
- 初探 Lambda Powertools TypeScript
- Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
- What is the difference between blue-green publishing, rolling publishing and gray publishing?
- Analysis and understanding of atomicintegerarray source code
猜你喜欢
33 million IOPs, 39 microsecond delay, carbon footprint certification, who is serious?
STM32 learning record 0007 - new project (based on register version)
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
初探 Lambda Powertools TypeScript
Special test 05 · double integral [Li Yanfang's whole class]
Multithreading
记录一个奇怪的bug:缓存组件跳转之后出现组件复制
Port occupied 1
1256:献给阿尔吉侬的花束
Express middleware ③ (custom Middleware)
随机推荐
Solution of discarding evaluate function in surprise Library
Special test 05 · double integral [Li Yanfang's whole class]
Choreographer full resolution
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
大专的我,闭关苦学 56 天,含泪拿下阿里 offer,五轮面试,六个小时灵魂拷问
RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
Kettle--控件解析
MySQL [read / write lock + table lock + row lock + mvcc]
Android interview theme collection
JMeter pressure test tool
Analysis of cluster component gpnp failed to start successfully in RAC environment
Ora-16047 of a DG environment: dgid mismatch between destination setting and target database troubleshooting and listening vncr features
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
JS 力扣刷题 102. 二叉树的层序遍历
Leetcode? The first common node of two linked lists
【报名】TF54:工程师成长地图与卓越研发组织打造
初识go语言