NHibernate中的表达树

时间:2010-08-08 12:45:00

标签: c# linq nhibernate expression-trees

我有一个具有此签名的方法

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

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

所以当类用户传递类似:

c => c.fullName == "John" && c.lastName == "Smith" && c.lastName != "" || c.fullName != ""  && c.Age > 18

我从表达式树中读取这个结构,这样我就有了一个完整的intellisense方法来提供搜索条件

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

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

c=>c.fullname = "John" 

我将从表达式树中提取以下信息:

propertyname = fullname , value = "John" , restrictiontype = "equality" 

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

ICriteria crit = session.CreateCriteria(typeof(T));
                    crit.Add(Restrictions.Eq(propretyName, value));
    IList<T> list = crit.Add(List<T>())
                    return list;

问题是它很难从expressiontree中读取,所以我想知道你们是否有任何简单的方法可能在expressiontree里面迭代来提取数据,或者你们有一些代码可以从ExpressionTree中检索数据。 / p>

1 个答案:

答案 0 :(得分:6)

以下是一些检索您提到的信息的代码。我相信你可以扩展它以包含你可能正在寻找的其他信息。

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);
}
相关问题