【问题标题】:Homework: HowTo obtain multiple keys collection?作业:如何获得多个密钥集合?
【发布时间】:2011-01-24 15:49:06
【问题描述】:

我有

public class Letter
{
    public string Value;
    public int Id;

    public Letter(string val, int id)
    {
        this.Value = val;
        this.Id = id;
    }
}

我需要一种重复的字典 (LookUp(?)) 用于:

private something TestCollection()
{
    List<Letter> inputList = new List<Letter> { 
        new Letter("a", 9), 
        new Letter("b", 5), 
        new Letter("c", 8), 
        new Letter("aIdentic", 9) 
    };

    // compare inputList by letter's ID(!)
    // use inputList (zero based) INDEXES as values

    // return something, like LookUp: { "a"=>(0, 3), "b"=>(1), "c"=>(2) };

}

使用 .NET 4

如何获得?

据我了解,有两种解决方案,一种来自 .NET 4,Lookup&lt;Letter, int&gt;,另一种,经典的一种Dictionary&lt;Letter, List&lt;int&gt;&gt;

谢谢。

编辑:

用于输出。有 2 个字母“a”,由数组中索引“0”上的 ID 9 标识(第一个位置)。 "b" 有索引 1(输入数组中的第二个位置),"c" - 索引 2(第三个)。

编辑 2

约翰解决方案:

    public class Letter
    {
        public string Value;
        public int Id;

        public Letter(string val, int id)
        {
            this.Value = val;
            this.Id = id;
        }
    }

    private void btnCommand_Click(object sender, EventArgs e)
    {
        List<Letter> inputList = new List<Letter> { 
            new Letter("a", 9), 
            new Letter("b", 5), 
            new Letter("c", 8), 
            new Letter("aIdentic", 9) 
        };

        var lookup = inputList.Select((value, index) =>
            new { value, index }).ToLookup(x => x.value, x => x.index);

        // outputSomething { "a"=>(0, 3), "b"=>(1), "c"=>(2) };
        foreach (var item in lookup)
        {
            Console.WriteLine("{0}: {1}", item.Key, item.ToString());
        }

    }

输出(我预计不超过 3 个键):

//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]
//WindowsFormsApplication2.Form1+Letter: System.Linq.Lookup`2+Grouping[WindowsFormsApplication2.Form1+Letter,System.Int32]

编辑 3 等于

public override bool Equals(object obj)
{
    if (obj is Letter)
        return this.Id.Equals((obj as Letter).Id);
    else
        return base.Equals(obj);
}

public override int  GetHashCode()
{
    return this.Id;
}

【问题讨论】:

  • 你的最后一个例子有什么问题,即字典?你试过了吗?听起来您需要每个键多个值,而不是问题所暗示的每个值多个键。
  • @chibacity:是的。字典中有多个键,因此每个键对应一个值数组。
  • @serhio 列表字典是一个很好的解决方案。单个键可以引用包含多个项目的列表。
  • @chibacity:也许吧。问题是如何获得它:)
  • @serhio 恐怕“获得它”并不完全清楚。你到底有什么问题?

标签: .net linq collections


【解决方案1】:

Lookup 可能是在这里使用的正确类 - LINQ 允许您使用 ToLookup 构建一个。请注意,Lookup 是在 .NET 3.5 中引入的,而不是在 .NET 4 中引入的。

话虽如此,目前还不清楚您将如何从输入到示例输出...

编辑:好的,既然我知道你在索引之后,你可能想使用 Select 的重载,它首先包含一个索引:

var lookup = inputList.Select((value, index) => new { value, index })
                      .ToLookup(x => x.value.Id, x => x.index);

【讨论】:

  • 我知道存在这样的功能,但不知道如何在我的情况下应用。一个小例子会很酷。
  • @serhio:啊,按索引。所以价值不重要?这将有助于更清楚......将编辑我的答案。
  • @serhio:啊,我明白了——我没有读到 9 是 ID,而不是“a”。现在和你在一起。我习惯于 ID 通常在 值之前表示......而对于“字母”,我希望使用文本 ID。话虽如此,我的回答仍然合适 - 您的示例代码使用 x =&gt; x.value,而不是像我的那样使用 x =&gt; x.value.Id
  • @serhio:老实说,“Foo”实际上会更清晰 - 它不会暗示文本部分与其 ID 之间存在如此强的相关性。 (我希望您的真实代码不使用公共字段,请注意...)
  • @serhio:因为结果不是Lookup&lt;Letter, int&gt; - 它是Lookup&lt;int, int&gt;。您的 ID 类型为 int,而不是 Letter。如果您希望能够通过Letter 进行查找,您可以去掉“.ID”并在Letter 本身中实现IEquatable&lt;Letter&gt;(或者只是覆盖Equals 和GetHashCode)。
猜你喜欢
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2015-06-24
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多