有两个主键的哈希表

发布于 2024-10-12 03:54:53 字数 116 浏览 3 评论 0原文

使用 System.Collections 如何创建具有两个主键的集合?

我的意思是避免使用具有相同组合的新条目,但每个键可以与其他键一起使用(例如在 SQL 中组合两个主键)

Using System.Collections how to create a collection with two primary keys ?

I mean new entries with the same combination are avoided but each key can be used with other keys (like combining two primary keys in SQL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光的暖冬 2024-10-19 03:54:53

您可以简单地使用struct,例如:

struct CompositeKey<T1,T2>
{
  public T1 Item1;
  public T2 Item2;
}

然后使用它作为键。

You can simply use a struct, example:

struct CompositeKey<T1,T2>
{
  public T1 Item1;
  public T2 Item2;
}

Then use that as the key.

歌入人心 2024-10-19 03:54:53

如果您使用的是 .NET 4.0,则可以使用 Tuple

否则你可以自己创建一个元组。

在 StackOverFlow 上找到:元组(或数组)作为 C# 中的字典键

struct Tuple<T, U, W> : IEquatable<Tuple<T,U,W>>
{
    readonly T first;
    readonly U second;
    readonly W third;

    public Tuple(T first, U second, W third)
    {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T First { get { return first; } }
    public U Second { get { return second; } }
    public W Third { get { return third; } }

    public override int GetHashCode()
    {
        return first.GetHashCode() ^ second.GetHashCode() ^ third.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }
        return Equals((Tuple<T, U, W>)obj);
    }

    public bool Equals(Tuple<T, U, W> other)
    {
        return other.first.Equals(first) && other.second.Equals(second) && other.third.Equals(third);
    }
}

You can use Tuple if you're using .NET 4.0.

Else you can create a Tuple by yourself.

Found on StackOverFlow : Tuples( or arrays ) as Dictionary keys in C#

struct Tuple<T, U, W> : IEquatable<Tuple<T,U,W>>
{
    readonly T first;
    readonly U second;
    readonly W third;

    public Tuple(T first, U second, W third)
    {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    public T First { get { return first; } }
    public U Second { get { return second; } }
    public W Third { get { return third; } }

    public override int GetHashCode()
    {
        return first.GetHashCode() ^ second.GetHashCode() ^ third.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        if (obj == null || GetType() != obj.GetType())
        {
            return false;
        }
        return Equals((Tuple<T, U, W>)obj);
    }

    public bool Equals(Tuple<T, U, W> other)
    {
        return other.first.Equals(first) && other.second.Equals(second) && other.third.Equals(third);
    }
}
墨小沫ゞ 2024-10-19 03:54:53

就像 LaGrandMere 所说,如果你'关于 .NET 4.0 或更高版本:

Tuple<int,string> key = Tuple.Create(0, "Test");

另外,请注意,如果您将字符串、整数等作为字典中的键,则必须对 SQL 中为 NULL 的内容进行特殊处理。字典中不能有空键。

var dict = new Dictionary<Tuple<string, int>, string>();

var address1 = Tuple.Create("5th Avenue",15);
var address2 = Tuple.Create("5th Avenue",25);
var address3 = Tuple.Create("Dag Hammarskjölds väg", 4);

dict[address1] = "Donald";
dict[address2] = "Bob";
dict[address3] = "Kalle";

// ...

int number = Int32.Parse("25");
var addressKey = Tuple.Create("5th Avenue",number);
string name = dict[addressKey]; // Bob

Like LaGrandMere said, you can use System.Tuple if you're on .NET 4.0 or later:

Tuple<int,string> key = Tuple.Create(0, "Test");

Also, note that if you're putting strings, ints etc as keys in dictionaries you're going to have to special-case what would've been NULL in SQL. Can't have a null-key in a Dictionary.

var dict = new Dictionary<Tuple<string, int>, string>();

var address1 = Tuple.Create("5th Avenue",15);
var address2 = Tuple.Create("5th Avenue",25);
var address3 = Tuple.Create("Dag Hammarskjölds väg", 4);

dict[address1] = "Donald";
dict[address2] = "Bob";
dict[address3] = "Kalle";

// ...

int number = Int32.Parse("25");
var addressKey = Tuple.Create("5th Avenue",number);
string name = dict[addressKey]; // Bob
木槿暧夏七纪年 2024-10-19 03:54:53

您还可以构造复合键并在字典中使用它

var compositeKey = key1.ToString()+key2.ToString();

var dict = new Dictionary<string,object>();
dict.Add(compositekey,val);

you can also construct composite key and use that in dictionary

var compositeKey = key1.ToString()+key2.ToString();

var dict = new Dictionary<string,object>();
dict.Add(compositekey,val);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文