当前位置:网站首页>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
边栏推荐
- 趣谈网络协议
- Oracle RAC database instance startup exception analysis IPC send timeout
- MySQL [read / write lock + table lock + row lock + mvcc]
- JS brain burning interview question reward
- AtCoder Beginner Contest 248C Dice Sum (生成函数)
- json反序列化匿名数组/对象
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
- 力扣刷题 101. 对称二叉树
- RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
- Oracle alarm log alert Chinese trace and trace files
猜你喜欢

Jenkins construction and use

Choreographer full resolution

JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes

Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)

Decentralized Collaborative Learning Framework for Next POI Recommendation

Express ② (routage)

专题测试05·二重积分【李艳芳全程班】

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

freeCodeCamp----time_ Calculator exercise
![[VMware] address of VMware Tools](/img/0e/13f263bd69c8224f7c755258d94777.png)
[VMware] address of VMware Tools
随机推荐
Troubleshooting of expdp export error when Oracle table has logical bad blocks
YARN线上动态资源调优
Port occupied 1
STM32 learning record 0007 - new project (based on register version)
Analysis and understanding of atomicintegerarray source code
自动化的艺术
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
解决方案架构师的小锦囊 - 架构图的 5 种类型
收藏博客贴
Leetcode? The first common node of two linked lists
Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
Leetcode brush question 897 incremental sequential search tree
Postman reference summary
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Express②(路由)
剑南春把文字游戏玩明白了
STM32学习记录0007——新建工程(基于寄存器版)
Express middleware ③ (custom Middleware)