[英]c# Lookup for a value in Dictionary of Dictionary
I am in process of implementing design where information is organized in following way:我正在实施设计,其中信息按以下方式组织:
I thought of coding it as:我想把它编码为:
// MasterDB :(0..m)SourceID -- 1..n -> [CaseID, InfoObject]
//
private Dictionary<string, Dictionary<string, InfoObject>> MasterDB;
Class InfoObject
{
string user;
}
This way addition and removal becomes very easy as they use SourceID and CaseID as key.这种方式添加和删除变得非常容易,因为他们使用 SourceID 和 CaseID 作为键。
However lookup is little special.但是查找有点特别。 I want to lookup for a particular user (which is embedded inside the InfoObject).
我想查找特定用户(嵌入在 InfoObject 中)。
How should I reorganize such that things become little efficient both for lookup and addition/removal?我应该如何重新组织以使查找和添加/删除的效率都变得很低?
UPDATE:更新:
I tried a different approach using LINQ我尝试了使用 LINQ 的不同方法
var targetList = from entry in MasterDB
from entrytarget in entry.Value
where (entrytarget.Value.user == username)
select entrytarget.Value;
Minor issue is that the returned list is a IEnumerable list.小问题是返回的列表是一个 IEnumerable 列表。 Not sure if I can make LINQ output some other way.
不确定我是否可以通过其他方式制作 LINQ output。
you could just have a separate lookup for users:您可以单独查找用户:
Dictionary<string, InfoObject> userLookup;
This only of course if you want to optimize for lookup speed, the downside is that addition and removal you have to do now on two separate data structures which have to keep in sync.当然,这只有在您想优化查找速度时,缺点是您现在必须对两个必须保持同步的独立数据结构进行添加和删除。
I would propose to maintain additionally another dictionary, mapping the user
into the appropriate collection of all relevant SourceID
/ CaseID
pairs.我建议另外维护另一个字典,将
user
映射到所有相关SourceID
/ CaseID
对的适当集合中。
I would use a Dictionary<string, IList<InfoObject>
since nothing guarantees a user can't have more than one SourceID/CaseID pair.我会使用
Dictionary<string, IList<InfoObject>
因为没有什么能保证用户不能拥有多个 SourceID/CaseID 对。
Whenever you insert something in your other dictionary, you also insert the same InfoObject
s in that dictionary of IList.每当您在其他字典中插入某些内容时,您也会在 IList 的该字典中插入相同的
InfoObject
。 This way the InfoObject
retrieved is the same object.这样检索到的
InfoObject
与 object 相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.