当前位置:网站首页>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
边栏推荐
- 计算饼状图百分比
- UWA Pipeline 功能详解|可视化配置自动测试
- PostgreSQL列存与行存
- Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
- Flask如何在内存中缓存数据?
- 安装及管理程序
- PyMySQL
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
- STM32__03—初识定时器
猜你喜欢
New project of OMNeT learning
Selenium IDE and XPath installation of chrome plug-in
Pytorch: the pit between train mode and eval mode
NVIDIA graphics card driver error
Getting started with JDBC
Xinwangda: HEV and Bev super fast charging fist products are shipped on a large scale
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
Derivation of Σ GL perspective projection matrix
TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
OMNeT学习之新建工程
随机推荐
_ Mold_ Board_
Log4j output log information to file
Loggie source code analysis source file module backbone analysis
Deeply understand the relevant knowledge of 3D model (modeling, material mapping, UV, normal), and the difference between displacement mapping, bump mapping and normal mapping
On the security of key passing and digital signature
oracle 中快速获取表的列名列表
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
Flask如何在内存中缓存数据?
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
Nodejs installation and environment configuration
The new MySQL table has a self increasing ID of 20 bits. The reason is
MySQL restores data through binlog file
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
ByteVCharts可视化图表库,你想要的我都有
NVIDIA显卡驱动报错
◰ GL shader handler encapsulation
New project of OMNeT learning
PyMySQL
Feign report 400 processing