If I have this ConcurrentDictionary:
public class User
{
public string Context { get; set; }
public bool Owner { get; set; }
}
protected static ConcurrentDictionary<User, string> OnlineUsers = new ConcurrentDictionary<User, string>();
Does anyone know how I would get the value of Owner if I already have the value of the Context? Basically I want to do a “find” using the Context. Thanks.
It sounds like you need a
Dictionary<string, User>which, when given the context as astringwill tell you whichUserit corresponds to. If you are going to need to perform this lookup several times, and there isn’t a problem using the additional memory, it may be worth creating such a dictionary.If you are only going to be doing the search once or twice, or the mappings will be changing so often that you can’t keep the dictionary up to date, then you can simply do a linear search on the dictionary using a
foreachloop, or using a LINQ method such asFirstorWhere, as demonstrated in other answers.