Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get dictionary key if value contains string

Tags:

c#

.net

I have this Dictionary:

    static Dictionary<int, string> Players = new Dictionary<int, string>();
    dictionary.Add(1, "Chris [GC]");
    dictionary.Add(2, "John");
    dictionary.Add(3, "Paul");
    dictionary.Add(4, "Daniel [GC]");

I want to get the key of values that contains "[GC]"

Any idea how?

Thanks.

like image 687
Brini Avatar asked Dec 28 '25 00:12

Brini


1 Answers

Use LINQ:

var result = Players.Where(p => p.Value.Contains("[GC]")).Select(p => p.Key);
like image 72
Kirill Polishchuk Avatar answered Dec 30 '25 16:12

Kirill Polishchuk



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!