当前位置:网站首页>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
边栏推荐
- Using Jupiter notebook in virtual environment
- AtomicIntegerArray源码分析与感悟
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
- elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
- 第十五章 软件工程新技术
- 项目中遇到的问题(五)操作Excel接口Poi的理解
- Wechat applet
- UNIX final exam summary -- for direct Department
- How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
- Android interview theme collection
猜你喜欢

【项目】小帽外卖(八)

Building MySQL environment under Ubuntu & getting to know SQL

蓝绿发布、滚动发布、灰度发布,有什么区别?

Leetcode | 38 appearance array

联想产品经理林林:天津当地网络运营商网络故障 ZUI系统后台服务器暂时无法正常工作

Leetcode brush question 897 incremental sequential search tree

SQL learning | set operation

JS 烧脑面试题大赏

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

Express中间件③(自定义中间件)
随机推荐
About me
Oracle alarm log alert Chinese trace and trace files
Force deduction brush question 101 Symmetric binary tree
Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
Leetcode brush question 𞓜 13 Roman numeral to integer
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
蓝绿发布、滚动发布、灰度发布,有什么区别?
JS force deduction brush question 102 Sequence traversal of binary tree
YARN线上动态资源调优
Es introduction learning notes
Postman reference summary
json反序列化匿名数组/对象
剑南春把文字游戏玩明白了
[code analysis (2)] communication efficient learning of deep networks from decentralized data
Problems encountered in the project (V) understanding of operating excel interface poi
Pytorch 经典卷积神经网络 LeNet
Small case of web login (including verification code login)
Analysis of unused index columns caused by implicit conversion of timestamp
JS 烧脑面试题大赏
[code analysis (5)] communication efficient learning of deep networks from decentralized data