0

I imagine there is a very concise way to do this, but I'm new to .NET. I have a bunch of pairs for a mapping. The mapping works both ways though. I'd like to store them once, like this:

{"a", "a'"},
{"b", "b'"},
...

using a Dictionary or something. I know I can use linq to easily query a dictionary, but how can I conditionally have it return the key if I query a value or return the value if I input a key? For example, if input b', output b. If input a, output a'.

The data structure does not have to be a dictionary by the way. I just want the most concise way to store it and the most concise way to retrieve it. This is not performance intensive.

4

2 回答 2

3

最简洁的方法是使用 Dictionary 并将所有内容放入其中,例如:

{ "a", "a'" }
{ "a'", "a" }
{ "b", "b'" }
{ "b'", "b" }

查找和使用将快速而清晰

于 2013-10-02T01:38:53.697 回答
2

创建字典扩展方法。

public static string Retrieve(this Dictionary<string,string> dictionary, string value)
    {
        var item = (from v in dictionary
                    where v.Key == value || v.Value == value
                    select (v.Key == value) ? v.Value : v.Key
                    ).FirstOrDefault();

        return item;
    }
于 2013-10-02T01:32:28.823 回答