表达式树 [英] Expression Trees
问题描述
我有这个signture的方法
I have a method which have this signture
public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
我用它来传递lambda表达式和由expressiontree reteriving数据使搜索限制在NHibernate的
i use to pass lambda expressions and make search restriction in nhibernate by reteriving data from expressiontree
所以,当类用户经过是这样的:
so when class user pass something like :
c => c.fullName == "John" && c.lastName == "Smith" && c.lastName != "" || c.fullName != "" && c.Age > 18
我得到读取表达式树这种结构,这样,我有一个完全智能感知的方式提供搜索标准
i get to read this structure from expression tree , that way i have a fully intellisense way to provide searching criteria
换句话说
我需要通过搜索标准,数据访问层(NHibernate的)
i need to pass searching criteria to data access layer(Nhibernate)
所以我需要提取表达式树的条件,然后将它传递到n举例休眠:
so i need to extract criteria from expression tree and then pass it to n hibernate by example :
c=>c.fullname = "John"
我将提取以下从表达式树的信息:
i will extract the following information from the expression tree :
PROPERTYNAME =全名,值=约翰,restrictiontype =平等,然后通过这个信息NHibernate的如下:
propertyname = fullname , value = "John" , restrictiontype = "equality" and then pass this info to nhibernate as following :
ICriteria crit = session.CreateCriteria(typeof(T));
crit.Add(Restrictions.Eq(propretyName, value));
IList<T> list = crit.Add(List<T>())
return list;
任何方式的问题是它真的很难从expressiontree阅读,所以我在想,如果你们有内部expressiontree也许迭代来提取数据,或者你们有一些代码reterive从ExpressionTree数据没有简单的方法。
any way the problem is its really hard to read from expressiontree ,so i was wondering if you guys have any easy way of maybe iterating inside expressiontree to pull data, or maybe you guys have some code to reterive data from ExpressionTree.
希望,使人们更清楚
在此先感谢
推荐答案
下面是一些获取你所提到的信息的代码。我敢肯定,你可以扩展到包括你可能会寻找更多的信息。
Here is some code that retrieves the information you mentioned. I’m sure you can extend this to include additional information you might be looking for.
public class Criterion
{
public string PropertyName;
public object Value;
public ExpressionType RestrictionType;
}
[....]
public static IEnumerable<Criterion> GetCriteria<T>(Expression<Func<T, bool>> expression)
{
return getCriteria<T>(expression.Body);
}
private static IEnumerable<Criterion> getCriteria<T>(Expression expression)
{
if (expression is BinaryExpression)
{
var bin = (BinaryExpression) expression;
if (bin.NodeType == ExpressionType.And || bin.NodeType == ExpressionType.AndAlso ||
bin.NodeType == ExpressionType.Or || bin.NodeType == ExpressionType.OrElse)
return getCriteria<T>(bin.Left).Concat(getCriteria<T>(bin.Right));
if (bin.Left is MemberExpression)
{
var me = (MemberExpression) bin.Left;
if (!(bin.Right is ConstantExpression))
throw new InvalidOperationException("Constant expected in criterion: " + bin.ToString());
return new[] { new Criterion {
PropertyName = me.Member.Name,
Value = ((ConstantExpression) bin.Right).Value,
RestrictionType = bin.NodeType
} };
}
throw new InvalidOperationException("Unsupported binary operator: " + bin.NodeType);
}
throw new InvalidOperationException("Unsupported expression type: " + expression.GetType().Name);
}
这篇关于表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!