使用 MultiValueDictionary 而不是带有 NHibernate.Criterion.Restrictions 的字典
Use MultiValueDictionary instead of Dictionary with NHibernate.Criterion.Restrictions
目前,我有这段代码,它可以工作,获取食物列表:
var myDictionary = new System.Collections.Generic.Dictionary<string, object>();
myDictionary .Add("RefId",1);
myDictionary.Add("Type", "fruit");
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, NHibernate.Criterion.Restrictions.AllEq(myDictionary));
我想使用MultiValueDictionary来收集不止一种类型。
var multiValueDictionary = new System.Collections.Generic.MultiValueDictionary<string, object>();
multiValueDictionary.Add("RefId",1);
multiValueDictionary.Add("Type", "fruit");
multiValueDictionary.Add("Type", "vegetable");
multiValueDictionary.Add("Type", "fungus");
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, NHibernate.Criterion.Restrictions.AllEq(multiValueDictionary));
最后一行没有编译,因为 MultiValueDictionary 没有实现 IDictionary,给出了错误:
Error CS1503 Argument 1: cannot convert from
'System.Collections.Generic.MultiValueDictionary' to
'System.Collections.IDictionary'
我该怎么做?
/// <summary>
/// Apply an "equals" constraint to each property in the key set of a IDictionary
/// </summary>
/// <param name="propertyNameValues">a dictionary from property names to values</param>
/// <returns></returns>
public static AbstractCriterion AllEq(IDictionary propertyNameValues)
{
Conjunction conj = Conjunction();
foreach (DictionaryEntry item in propertyNameValues)
{
conj.Add(Eq(item.Key.ToString(), item.Value));
}
return conj;
}
因此,对于您的多值案例,您可以停止将其放入字典中,然后手动添加:
var myDictionary = new Dictionary<string, object>();
myDictionary.Add("RefId", 1);
var filter = (Conjunction)Restrictions.AllEq(myDictionary);
filter.Add(Restrictions.In("Type", new [] {"fruit", "vegetable", "fungus"});
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, filter);
当然,如果你现在只有RefId
可以放入你的字典,最好简化它:
var filter = Restrictions.Conjunction();
filter.Add(Restrictions.Eq("RefId", 1));
filter.Add(Restrictions.In("Type", new [] {"fruit", "vegetable", "fungus"}));
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, filter);
现在,如果您想要 AllIn
处理多值,请对其进行编码:
public static AbstractCriterion AllIn<TValue>(
MultiValueDictionary<string, TValue> propertyNameValues)
{
var conj = Restrictions.Conjunction();
foreach (KeyValuePair<TKey, IReadOnlyCollection<TValue>> item in propertyNameValues)
{
if (item.Value.Count > 1)
{
conj.Add(Restrictions.In(item.Key, item.Value.ToArray()));
}
else
{
conj.Add(Restrictions.Eq(item.Key, item.Value.FirstOrDefault()));
}
}
return conj;
}
目前,我有这段代码,它可以工作,获取食物列表:
var myDictionary = new System.Collections.Generic.Dictionary<string, object>();
myDictionary .Add("RefId",1);
myDictionary.Add("Type", "fruit");
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, NHibernate.Criterion.Restrictions.AllEq(myDictionary));
我想使用MultiValueDictionary来收集不止一种类型。
var multiValueDictionary = new System.Collections.Generic.MultiValueDictionary<string, object>();
multiValueDictionary.Add("RefId",1);
multiValueDictionary.Add("Type", "fruit");
multiValueDictionary.Add("Type", "vegetable");
multiValueDictionary.Add("Type", "fungus");
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, NHibernate.Criterion.Restrictions.AllEq(multiValueDictionary));
最后一行没有编译,因为 MultiValueDictionary 没有实现 IDictionary,给出了错误:
Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.MultiValueDictionary' to 'System.Collections.IDictionary'
我该怎么做?
/// <summary> /// Apply an "equals" constraint to each property in the key set of a IDictionary /// </summary> /// <param name="propertyNameValues">a dictionary from property names to values</param> /// <returns></returns> public static AbstractCriterion AllEq(IDictionary propertyNameValues) { Conjunction conj = Conjunction(); foreach (DictionaryEntry item in propertyNameValues) { conj.Add(Eq(item.Key.ToString(), item.Value)); } return conj; }
因此,对于您的多值案例,您可以停止将其放入字典中,然后手动添加:
var myDictionary = new Dictionary<string, object>();
myDictionary.Add("RefId", 1);
var filter = (Conjunction)Restrictions.AllEq(myDictionary);
filter.Add(Restrictions.In("Type", new [] {"fruit", "vegetable", "fungus"});
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, filter);
当然,如果你现在只有RefId
可以放入你的字典,最好简化它:
var filter = Restrictions.Conjunction();
filter.Add(Restrictions.Eq("RefId", 1));
filter.Add(Restrictions.In("Type", new [] {"fruit", "vegetable", "fungus"}));
var foodList = this.factoryService.GetMyService()
.GetByCriteria(sortOrder, filter);
现在,如果您想要 AllIn
处理多值,请对其进行编码:
public static AbstractCriterion AllIn<TValue>(
MultiValueDictionary<string, TValue> propertyNameValues)
{
var conj = Restrictions.Conjunction();
foreach (KeyValuePair<TKey, IReadOnlyCollection<TValue>> item in propertyNameValues)
{
if (item.Value.Count > 1)
{
conj.Add(Restrictions.In(item.Key, item.Value.ToArray()));
}
else
{
conj.Add(Restrictions.Eq(item.Key, item.Value.FirstOrDefault()));
}
}
return conj;
}