NHibernate 中的表达式树 [英] Expression Trees in NHibernate

查看:28
本文介绍了NHibernate 中的表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有这个签名的方法

I have a method which have this signature

public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)

我过去常常通过从表达式树中检索数据来传递 lambda 表达式并在 nhibernate 中进行搜索限制.

I use to pass lambda expressions and make search restriction in nhibernate by retrieving 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 full intellisense way to provide searching criteria

换句话说:我需要将搜索条件传递给数据访问层(Nhibernate)

In other words: I need to pass searching criteria to data access layer (Nhibernate)

所以我需要从表达式树中提取条件,然后通过示例将其传递给 n hibernate :

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 = fullname , value = "John" , restrictiontype = "equality" 

然后将此信息传递给 nhibernate 如下:

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;

问题是从表达式树中读取数据真的很困难,所以我想知道你们是否有任何简单的方法可以在表达式树中进行迭代以提取数据,或者你们有一些代码可以从表达式树中检索数据.

Any way the problem is it's 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 retrieve 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);
}

这篇关于NHibernate 中的表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆