向字典添加 Like 限制
Add Like restriction to dictionary
我为项目 ID 设置了 nHibernate 限制,如下所示:
var attachmentProperties = new System.Collections.Generic.Dictionary<string, object>();
attachmentProperties.Add("Id", this.project.Id);
所以它 returns 任何与该项目 ID 相关的设置限制:
NHibernate.Criterion.Restrictions.AllEq(attachmentProperties));
这行得通。我现在想添加另一个限制,使用相当于 SQL Like。我试过了:
attachmentProperties.Add(NHibernate.Criterion.Restrictions.Like("Type", "%dog%"));
There is no argument given that corresponds to required formal
parameter value
我想获取类型包含 "dog" 的任何内容(除了 Id 匹配)。我该怎么做?
您有两个选择:
让所有 Dog 类 实现一个公共接口 (IDog)。然后,字典将是:
new System.Collections.Generic.Dictionary<string, IDog>();
或者,您可以执行此操作,这可能有点奇怪,如果在您验证 Add 方法时由于某种原因顶部的内容不可用。您同样必须检查其他方法。
class DogDictionary : Dictionary<string, object>
{
public virtual void Add(KeyValuePair<string, object> item)
{
if (item.Value.GetType().ToString().ToUpper().Contains("DOG"))
throw new ApplicationException("Invalid Data Type for Value. Should Have 'Dog' in the Object Name");
}
}
虽然我会选择第一个选项。
另一种方法是为条件创建 Restrictions.Conjunction
:
Conjunction conjunction = Restrictions.Conjunction();
conjunction.Add(Restrictions.Eq("Id", this.project.Id));
conjunction.Add(Restrictions.Like("Type", "%dog%"));
conjunction.Add(Restrictions.Not(Restrictions.Like("Type", "%cat%")));
我为项目 ID 设置了 nHibernate 限制,如下所示:
var attachmentProperties = new System.Collections.Generic.Dictionary<string, object>();
attachmentProperties.Add("Id", this.project.Id);
所以它 returns 任何与该项目 ID 相关的设置限制:
NHibernate.Criterion.Restrictions.AllEq(attachmentProperties));
这行得通。我现在想添加另一个限制,使用相当于 SQL Like。我试过了:
attachmentProperties.Add(NHibernate.Criterion.Restrictions.Like("Type", "%dog%"));
There is no argument given that corresponds to required formal parameter value
我想获取类型包含 "dog" 的任何内容(除了 Id 匹配)。我该怎么做?
您有两个选择:
让所有 Dog 类 实现一个公共接口 (IDog)。然后,字典将是:
new System.Collections.Generic.Dictionary<string, IDog>();
或者,您可以执行此操作,这可能有点奇怪,如果在您验证 Add 方法时由于某种原因顶部的内容不可用。您同样必须检查其他方法。
class DogDictionary : Dictionary<string, object>
{
public virtual void Add(KeyValuePair<string, object> item)
{
if (item.Value.GetType().ToString().ToUpper().Contains("DOG"))
throw new ApplicationException("Invalid Data Type for Value. Should Have 'Dog' in the Object Name");
}
}
虽然我会选择第一个选项。
另一种方法是为条件创建 Restrictions.Conjunction
:
Conjunction conjunction = Restrictions.Conjunction();
conjunction.Add(Restrictions.Eq("Id", this.project.Id));
conjunction.Add(Restrictions.Like("Type", "%dog%"));
conjunction.Add(Restrictions.Not(Restrictions.Like("Type", "%cat%")));