很容易从数据库中获取密钥的值.NET通用词典:

Dictionary<int, string> greek = new Dictionary<int, string>();
greek.Add(1, "Alpha");
greek.Add(2, "Beta");
string secondGreek = greek[2];  // Beta

但是,try 获取给定值的密钥并不那么简单,因为可能有多个密钥:

int[] betaKeys = greek.WhatDoIPutHere("Beta");  // expecting single 2

推荐答案

好的,这是多重双向版本:

using System;
using System.Collections.Generic;
using System.Text;

class BiDictionary<TFirst, TSecond>
{
    IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>();
    IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>();

    private static IList<TFirst> EmptyFirstList = new TFirst[0];
    private static IList<TSecond> EmptySecondList = new TSecond[0];

    public void Add(TFirst first, TSecond second)
    {
        IList<TFirst> firsts;
        IList<TSecond> seconds;
        if (!firstToSecond.TryGetValue(first, out seconds))
        {
            seconds = new List<TSecond>();
            firstToSecond[first] = seconds;
        }
        if (!secondToFirst.TryGetValue(second, out firsts))
        {
            firsts = new List<TFirst>();
            secondToFirst[second] = firsts;
        }
        seconds.Add(second);
        firsts.Add(first);
    }

    // Note potential ambiguity using indexers (e.g. mapping from int to int)
    // Hence the methods as well...
    public IList<TSecond> this[TFirst first]
    {
        get { return GetByFirst(first); }
    }

    public IList<TFirst> this[TSecond second]
    {
        get { return GetBySecond(second); }
    }

    public IList<TSecond> GetByFirst(TFirst first)
    {
        IList<TSecond> list;
        if (!firstToSecond.TryGetValue(first, out list))
        {
            return EmptySecondList;
        }
        return new List<TSecond>(list); // Create a copy for sanity
    }

    public IList<TFirst> GetBySecond(TSecond second)
    {
        IList<TFirst> list;
        if (!secondToFirst.TryGetValue(second, out list))
        {
            return EmptyFirstList;
        }
        return new List<TFirst>(list); // Create a copy for sanity
    }
}

class Test
{
    static void Main()
    {
        BiDictionary<int, string> greek = new BiDictionary<int, string>();
        greek.Add(1, "Alpha");
        greek.Add(2, "Beta");
        greek.Add(5, "Beta");
        ShowEntries(greek, "Alpha");
        ShowEntries(greek, "Beta");
        ShowEntries(greek, "Gamma");
    }

    static void ShowEntries(BiDictionary<int, string> dict, string key)
    {
        IList<int> values = dict[key];
        StringBuilder builder = new StringBuilder();
        foreach (int value in values)
        {
            if (builder.Length != 0)
            {
                builder.Append(", ");
            }
            builder.Append(value);
        }
        Console.WriteLine("{0}: [{1}]", key, builder);
    }
}

.net相关问答推荐

无法在Ubuntu 22.04.3上运行带有Rider 2023和DotNet-8.0的项目

.NET Core 中的微服务

CurrentCulture、InvariantCulture、CurrentUICulture 和 InstalledUICulture 之间的区别

如何解决请确保文件可访问并且它是有效的程序集或 COM 组件?

使用 IIS Express 托管网站(临时)

.NET 世界是否有 Maven 替代方案或端口?

Winforms:Application.Exit vs Environment.Exit vs Form.Close

使用多个 MemoryCache 实例

注册 COM 互操作与使程序集 COM 可见

序列化私有成员数据

如何在 C# 中序列化异常对象?

变量MyException已声明但从未使用

Convert.ToBoolean 和 Boolean.Parse 不接受 0 和 1

我不了解应用程序域

Environment.GetFolderPath(...CommonApplicationData) 在 Vista 上仍然返回C:\Documents and Settings\

在 C# 中使用 Bitmap 对象查找图像格式

C# - 在 WPF 应用程序中保存用户设置的方法?

在 IIS 中访问 .svc 文件时出现 HTTP 404

在类型 c# 上切换大小写

不签署 .NET 程序集有什么问题吗?