Getting a value from a ConcurrentDictionary

Joey Morani picture Joey Morani · Sep 21, 2012 · Viewed 12.4k times · Source

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.

Answer

Charles Prakash Dasari picture Charles Prakash Dasari · Sep 21, 2012

Does anything stop you from using standard Linq FirstOrDefault() method like so:

var item = OnlineUsers.FirstOrDefault(kvp => kvp.Key.Context == myContext);