[英]NHibernate Criteria Filtering
I use this code to filter database records 我使用此代码来过滤数据库记录
if (!string.IsNullOrEmpty(_searchCriteria.MessageType))
{
var messageType = (AutotransferMessageType)Enum.Parse(typeof(AutotransferMessageType), _searchCriteria.MessageType, true);
if (Enum.IsDefined(typeof(AutotransferMessageType), messageType))
{
criteriaQuery.CreateAlias("AutotransferInputRecord", "AutotransferInputRecord")
.Add(
Restrictions.Eq(
"AutotransferInputRecord." + AutotransferLogSearchCriteria.MessageTypePropertyName,
messageType));
}
else
{
criteriaQuery.Add(Restrictions.IsNull("AutotransferInputRecord"));
}
}
AutotransferMessageType
is enumerable type AutotransferMessageType
是可枚举的类型
public enum AutotransferMessageType
{
[DisplayName("MT202")]
[DatabaseName("MT202")]
MT202,
[DisplayName("MT210")]
[DatabaseName("MT210")]
MT210,
//...
}
My filter outputs the results when I enter MT202, for example. 例如,当我输入MT202时,我的过滤器就会输出结果。 (It's the right behavior).
(这是正确的行为)。
When I input just number, for example, 202, I get no results (It's the right behavior too). 当我仅输入数字(例如202)时,我没有任何结果(这也是正确的行为)。 But when I try to input some line, "mt", for example, I get error
但是,当我尝试输入某些行“ mt”时,出现错误
Unexpected application error has been occured: 发生了意外的应用程序错误:
'Requested value 'mt' was not found.' “找不到要求的值'mt'。”
How to make the filter do not show any results when I input a line? 输入行时如何使过滤器不显示任何结果?
Your error is coming from the line that parses the enum. 您的错误来自解析枚举的行。 Use Enum.TryParse instead:
使用Enum.TryParse代替:
AutotransferMessageType msgEnum;
var enumPrasedOk = Enum.TryParse(_searchCriteria.MessageType, true, out msgEnum);
if(enumPrasedOk){
//Do something
}else{
//Handle case where enum was not found for some reason (if need be)
}
Also please note that you can not look up the enum this way using it's description (in your case they are the same so it is ok). 另外请注意,您不能使用描述来以这种方式查找枚举(在您的情况下,它们是相同的,因此可以)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.