[펌] Get dictionary key by value
Programming/C# / 2024. 3. 6. 17:04
How do I get a Dictionary key by value in C#?
Dictionary<string, string> types = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
[Answer]
Values do not necessarily have to be unique, so you have to do a lookup. You can do something like this:
var myKey = types.FirstOrDefault(x => x.Value == "one").Key;
If values are unique and are inserted less frequently than read, then create an inverse dictionary where values are keys and keys are values.
[출처] https://stackoverflow.com/questions/2444033/get-dictionary-key-by-value
[참조] https://miuna3.tistory.com/81
반응형
'Programming > C#' 카테고리의 다른 글
[링크][C#] 양력 <-> 음력 변환 (0) | 2024.11.25 |
---|---|
[Exception] ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime. (0) | 2024.02.29 |
[링크] C# 특정 문자열 삭제, 특정 문자열 교체 Regex.Replace (0) | 2023.11.08 |
[펌] c# String.Format && string 자릿수 맞추기 (0) | 2023.09.18 |
[링크] [C#][.NET framework] Directory.GetFiles() 로 여러 확장자 필터링 하기 (0) | 2023.02.21 |