【发布时间】:2014-02-15 22:01:22
【问题描述】:
我正在尝试使用表达式创建动态休眠查询。我对 Contains、StartsWith 和 EndsWith 等函数没有任何问题,但我似乎无法让它与 IsNullOrEmpty 一起使用。这是一些上下文:
包含:
MethodInfo contains = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
ParameterExpression param = Expression.Parameter(typeof(MyType), "x");
Expression expression = null;
PropertyInfo info = typeof(MyType).Property("MyStringProperty");
expression = Expression.Property(param, info);
expression = Expression.Call(expression, info, Expression.Constant("Does it contain this string", typeof(string)));
在这一切的最后,表达式在调试器中等于这个:
expression = { x.MyStringProperty };
然后我把它变成一个 lambda 表达式:
var finalExpression = Expression.Lambda<Func<MyType, bool>>(expression, param);
最后,finalExpression是这样的:
x => x.MyStringProperty.Contains("Does it contain this string");
当我通过 nhibernate 运行它时,它完全符合我的要求。不过,有了 IsNullOrEmpty,这就是我所拥有的:
MethodInfo isNull = typeof(string).GetMethod("IsNullOrEmpty", new Type[] { typeof(string) } );
ParameterExpression param = Expression.Parameter(typeof(MyType), "x");
PropertyInfo info = typeof(MyType).GetProperty("MyStringProperty");
expression = Expression.Property(param, info);
expression = Expression.Call(isNull, expression);
最后,表达式等于:
expression = { IsNullOrEmpty(x.MyStringProperty) }
在 lambda 转换之后变成:
finalExpression = { x => IsNullOrEmpty(x) }
这看起来和它应该的完全一样(虽然我承认它可能应该读取 string.IsNullOrEmpty(x)),但是当我通过 nhibernate 运行它时,我得到了错误:
NotSupportedException
Message: Boolean IsNullOrEmpty(System.String)
有谁知道这是为什么?如果我运行一个非动态查询并像这样手写一个 where 子句,它就可以正常工作:
nhibernateDataProvider.Where(x => string.IsNullOrEmpty(x.MySTringProperty));
有人知道这是为什么/如何解决这个问题吗?
编辑:
我为什么要这样做:
在我的 Web 层中,我向用户显示了这个 x.MyStringProperty 数据。他们有能力通过这个属性进行过滤,我希望能够通过这些字符串方法来过滤数据。 Web 层只是将这些函数传回:
BeginsWith
EndsWith
Contains
IsNullOrEmpty
NotIsNullOrEmpty
从这些名称中,我可以使用上面的代码直接将它们转换为字符串方法。因此,我可以传递 Web 层中的任何类型,并能够过滤任何字符串属性。这非常强大,因为对于我所有的数百个类,我可以使用一种方法来处理我对每个类的过滤。正如我之前所说,IT 使用非静态方法,因为构建的表达式是:
x -> x.MyStringProperty.NonStaticMethod("SomeFilterValue");
在 Expression.Call 的文档中说得对,您也应该能够使用静态方法来执行此操作。由于它适用于非静态方法,因此必须有一种方法来执行 IsNullOrEmpty 使得表达式变为
x -> string.StaticMethod(x.MyStringProperty)
我敢打赌,如果我创建表达式,它会起作用
x -> x.MySTringProperty == null || x.MySTringProperty == ""
但这是一种解决方法,作为一名程序员,我觉得我需要找出如何使用静态方法来完成上述工作。
【问题讨论】:
标签: c# .net linq generics nhibernate