当前位置:网站首页>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
边栏推荐
- Kettle--控件解析
- UNIX final exam summary -- for direct Department
- SQL learning window function
- Solution of discarding evaluate function in surprise Library
- SQL learning | set operation
- Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
- Analysis of cluster component gpnp failed to start successfully in RAC environment
- 函数只执行第一次的执行一次 once函数
- The art of automation
- Redis docker 安装
猜你喜欢
随机推荐
Get the attribute value difference between two different objects with reflection and annotation
[code analysis (4)] communication efficient learning of deep networks from decentralized data
STM32 learning record 0007 - new project (based on register version)
Express②(路由)
UML统一建模语言
Atcoder beginer contest 248c dice sum (generating function)
OSS cloud storage management practice (polite experience)
scikit-learn構建模型的萬能模板
China creates vast research infrastructure to support ambitious climate goals
Question bank and answer analysis of the 2022 simulated examination of the latest eight members of Jiangxi construction (quality control)
1256:献给阿尔吉侬的花束
Es introduction learning notes
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
JS 力扣刷题 102. 二叉树的层序遍历
Move blog to CSDN
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
go 语言 数组,字符串,切片
趣谈网络协议
Express②(路由)
Introduction to spark basic operation