当前位置:网站首页>Expression "func" tSource, object "to expression" func "tSource, object" []
Expression "func" tSource, object "to expression" func "tSource, object" []
2022-04-23 16:53:00 【Tassel 1990】
// Handle multiple attribute generation Expression expression , from Expression<Func<TSource, object>> To Expression<Func<TSource, object>>[], Single to multiple
// from x=>new{x.a,x.b} To 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;
}
Source idea :
Get entities T Of all properties of lambda Expression array : Such as 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;
}
版权声明
本文为[Tassel 1990]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359253845.html
边栏推荐
- Nodejs installation and environment configuration
- MySQL restores data through binlog file
- Set cell filling and ranking method according to the size of the value in the soft report
- Deepinv20 installation MariaDB
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- Log4j output log information to file
- Change the password after installing MySQL in Linux
- STM32__03—初识定时器
- 英语 | Day15、16 x 句句真研每日一句(从句断开、修饰)
- Public variables of robotframework
猜你喜欢
Creation of RAID disk array and RAID5
Modify the test case name generated by DDT
SQL database
PyTorch:train模式与eval模式的那些坑
loggie 源码分析 source file 模块主干分析
Set cell filling and ranking method according to the size of the value in the soft report
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
RTKLIB 2.4.3源码笔记
Cartoon: what are IAAs, PAAS, SaaS?
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
随机推荐
How to build tiktok user trust and drive fan growth
JMeter installation tutorial and solutions to the problems I encountered
Talk about browser cache control
深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
How to implement distributed locks with redis?
Real time operation of vim editor
Introduction to how to set up LAN
DanceNN:字节自研千亿级规模文件元数据存储系统概述
CentOS MySQL multi instance deployment
Derivation of Σ GL perspective projection matrix
MySQL master-slave configuration under CentOS
Loading order of logback configuration file
LVM and disk quota
【PIMF】OpenHarmony啃论文俱乐部—在ACM Survey闲逛是什么体验
UWA Pipeline 功能详解|可视化配置自动测试
众昂矿业:萤石浮选工艺
Getting started with JDBC
建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
Change the password after installing MySQL in Linux
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme