[펌] Get dictionary key by value
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#' 카테고리의 다른 글
[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 |
[링크] [C#/.Net][String To DateTime] 문자형을 Datetime형으로 변경하기 (0) | 2022.10.06 |