如何使用 Dictionary/List<Document> 过滤此数据?

How to filter this data using Dictionary/List<Document>?


我的代码需要一些帮助。

这段代码是我写的。

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();



当我 运行 代码时,它说它有这个错误。

'System.Collections.Generic.KeyNotFoundException' 类型的异常发生在 mscorlib.dll 中,但未在用户代码中处理

附加信息:字典中不存在给定的键。

谁能帮我解决一下?

考虑到你提到的异常,我怀疑在你尝试访问它们之前应该检查对字典的调用,所以而不是

_allUsers[s.Get("ObjStylist")]

你可以试试

_allUsers.ContainsKey(s.Get("ObjStylist")) ? _allUsers[s.Get("ObjStylist")] : null

_allDepartments[s.Get("ObjDepartment")]_allProducts[s.Get("ObjProduct")] 也是如此。

问题是,您正在尝试查找字典中不存在的关键字。例如:_allUsers[s.Get("ObjStylist")] 如果 s.Get("ObjStylist") 包含一个不存在的密钥,您将收到此错误。

因此 TryGetValue() 字典的方法非常有用,因为你只查找关键的 (而不是使用 Contains()dict[key] )

您可以创建一个 returns 值存在的查找函数(这个通用函数适用于所有词典)

(此示例未经测试,可能需要一些调整)

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();