Sta*_*ker 2 c# linq nhibernate expression-trees
我有一个有这个签名的方法
public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
Run Code Online (Sandbox Code Playgroud)
我用来传递lambda表达式并通过从expressiontree中检索数据来在nhibernate中进行搜索限制.
所以当类用户传递类似于:
c => c.fullName == "John" && c.lastName == "Smith" && c.lastName != "" || c.fullName != "" && c.Age > 18
Run Code Online (Sandbox Code Playgroud)
我从表达式树中读取这个结构,这样我就有了一个完整的intellisense方法来提供搜索条件
换句话说:我需要将搜索条件传递给数据访问层(Nhibernate)
所以我需要从表达式树中提取标准,然后通过示例将其传递给n hibernate:
c=>c.fullname = "John"
Run Code Online (Sandbox Code Playgroud)
我将从表达式树中提取以下信息:
propertyname = fullname , value = "John" , restrictiontype = "equality"
Run Code Online (Sandbox Code Playgroud)
然后将此信息传递给nhibernate,如下所示:
ICriteria crit = session.CreateCriteria(typeof(T));
crit.Add(Restrictions.Eq(propretyName, value));
IList<T> list = crit.Add(List<T>())
return list;
Run Code Online (Sandbox Code Playgroud)
无论如何问题是它很难从expressiontree中读取,所以我想知道你们是否有任何简单的方法可能在expressiontree里面迭代来提取数据,或者你们有一些代码可以从ExpressionTree中检索数据.
以下是一些检索您提到的信息的代码.我相信你可以扩展它以包含你可能正在寻找的其他信息.
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1072 次 |
| 最近记录: |