I need some help here for my code.
I wrote this code.
List<Document> doc = SystemOperationManager.GetSalesByMemberLucene(ConfigurationManager.GetIndexPath(), memberId).ToList();
Dictionary<string, Department> _allDepartments = DepartmentManager.GetAll().ToDictionary(s => s.Id.ToString(), s => s);
Dictionary<string, User> _allUsers = UserManager.GetAll().ToDictionary(s => s.Id.ToString(), s => s);
Dictionary<string, Product> _allProducts = ProductManager.GetAll().Where(x => x.CustomType == 2).ToDictionary(s => s.Id.ToString(), s => s);
List<SystemOperation> so = doc.Select(s => new SystemOperation
{
ObjStylist = s.Get("ObjStylist") != null ? _allUsers[s.Get("ObjStylist")] : null,
ObjDepartment = s.Get("ObjDepartment") != null ? _allDepartments[s.Get("ObjDepartment")] : null,
ObjProduct = s.Get("ObjProduct") != null ? _allProducts[s.Get("ObjProduct")] : null
//TotalPointsCollected = decimal.Parse(s.Get("TotalPointsCollected")),
//PointsAccumulated = decimal.Parse(s.Get("PointsAccumulated"))
}).ToList();
_result = so;
rgList.DataSource = _result;
rgList.DataBind();
when I run the code it say it has this error.
An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll but was not handled in user code
Additional information: The given key was not present in the dictionary.
Anyone can help me fix it?
given the exception you mentioned i suspect the calls to the dictionaries should be checked before you try to access them, so instead of
_allUsers[s.Get("ObjStylist")]
you could try
_allUsers.ContainsKey(s.Get("ObjStylist")) ? _allUsers[s.Get("ObjStylist")] : null
the same goes for _allDepartments[s.Get("ObjDepartment")]
and _allProducts[s.Get("ObjProduct")]
.
The problem is, that you're trying to lookup for a key that doesn't exists in the dictionary. For example: _allUsers[s.Get("ObjStylist")]
if s.Get("ObjStylist")
contains a nonexisting key, you'll get this error.
Therefor a TryGetValue()
method of a dictionary is very usefull, because you're only looking up the key ones (instead of using Contains()
and dict[key]
)
You could create a lookup function which returns the value is it exists (this generic function will work on all dictionaries)
(this example is not tested and might need some tweaks)
private T LookupData<T>(Dictionary<string, T> dict, string key)
{
if(key == null)
return null;
T result;
if(dict.TryGetValue(key, out result))
return result;
else
return null;
}
List<Document> doc = SystemOperationManager.GetSalesByMemberLucene(ConfigurationManager.GetIndexPath(), memberId).ToList();
Dictionary<string, Department> _allDepartments = DepartmentManager.GetAll().ToDictionary(s => s.Id.ToString(), s => s);
Dictionary<string, User> _allUsers = UserManager.GetAll().ToDictionary(s => s.Id.ToString(), s => s);
Dictionary<string, Product> _allProducts = ProductManager.GetAll().Where(x => x.CustomType == 2).ToDictionary(s => s.Id.ToString(), s => s);
List<SystemOperation> so = doc.Select(s => new SystemOperation
{
ObjStylist = LookupData<User>(_allUsers, s.Get("ObjStylist")),
ObjDepartment = LookupData<Department>(_allDepartments, s.Get("ObjDepartment")),
ObjProduct = LookupData<Product>(_allProducts, s.Get("ObjProduct"))
//TotalPointsCollected = decimal.Parse(s.Get("TotalPointsCollected")),
//PointsAccumulated = decimal.Parse(s.Get("PointsAccumulated"))
}).ToList();
_result = so;
rgList.DataSource = _result;
rgList.DataBind();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.