当前位置:网站首页>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
边栏推荐
- Use if else to judge in sail software - use the title condition to judge
- 建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细
- 无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
- Selenium IDE and XPath installation of chrome plug-in
- The new MySQL table has a self increasing ID of 20 bits. The reason is
- MySql主从复制
- Nodejs installation and environment configuration
- Dlib of face recognition framework
- VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
- STM32__03—初识定时器
猜你喜欢
Selenium IDE and XPath installation of chrome plug-in
Path environment variable
扫码登录的原理你真的了解吗?
TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
Installation and management procedures
NVIDIA显卡驱动报错
PyTorch:train模式与eval模式的那些坑
Zhongang Mining: Fluorite Flotation Process
Use case execution of robot framework
feign报400处理
随机推荐
◰ GL shadow map core steps
The font of the soft cell changes color
DDT + Excel for interface test
安装及管理程序
Rtklib 2.4.3 source code Notes
Paging the list collection
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
How to build tiktok user trust and drive fan growth
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
How magical is the unsafe class used by all major frameworks?
On the security of key passing and digital signature
伪分布安装spark
Feign report 400 processing
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
BUG_ me
SQL database
Dlib of face recognition framework
Linux MySQL data timing dump
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
Change the password after installing MySQL in Linux