Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a value from a ConcurrentDictionary

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.

like image 427
Joey Morani Avatar asked Jul 05 '26 00:07

Joey Morani


2 Answers

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

var item = OnlineUsers.FirstOrDefault(kvp => kvp.Key.Context == myContext);
like image 138
Charles Prakash Dasari Avatar answered Jul 08 '26 05:07

Charles Prakash Dasari


How obout something like

var ou = OnlineUsers.First(x => x.Key.Context == "TADA");
like image 28
Adriaan Stander Avatar answered Jul 08 '26 05:07

Adriaan Stander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!