【问题标题】:Convert List<T> to HashTable将 List<T> 转换为 HashTable
【发布时间】:2013-01-07 22:12:01
【问题描述】:

我有一个清单:

public class tmp
{
    public int Id;
    public string Name;
    public string LName;
    public decimal Index;
}

List<tmp> lst = GetSomeData();

我想将此列表转换为哈希表,并且我想在扩展方法参数中指定KeyValue。例如我可能想要Key=IdValue=IndexKey = Id + IndexValue = Name + LName。我怎样才能做到这一点?

【问题讨论】:

  • 您是否出于遗留原因使用 HashTable(例如,使用某些第三方或旧代码)?如果没有,请改用 Dictionary,正如其他评论者所指出的那样。
  • @AppDeveloper 这绝对是我第一次看到有人在问题的 cmets 中链接他们的答案。

标签: c# linq c#-4.0 hashtable extension-methods


【解决方案1】:

你可以使用ToDictionary方法:

var dic1 = list.ToDictionary(item => item.Id, 
                             item => item.Name);

var dic2 = list.ToDictionary(item => item.Id + item.Index, 
                             item => item.Name + item.LName);

您不需要使用来自 .NET 1.1 的 HashtableDictionary 更安全。

【讨论】:

    【解决方案2】:

    在 C# 4.0 中你可以使用Dictionary&lt;TKey, TValue&gt;:

    var dict = lst.ToDictionary(x => x.Id + x.Index, x => x.Name + x.LName);
    

    但如果你真的想要一个Hashtable,请将该字典作为参数传递给HashTable 构造函数...

    var hashTable = new Hashtable(dict);
    

    【讨论】:

      【解决方案3】:

      您可以使用ToDictionary 扩展方法并将结果字典传递给Hashtable 构造函数:

      var result = new Hashtable(lst.ToDictionary(e=>e.Id, e=>e.Index));
      

      【讨论】:

      • 这很好用,但显然它会为字典制作一个额外的数据副本 - 对于大型数据集,这可能是一个问题。幸运的是,采用 IDictionary 的 Hashtable 构造函数只是一个 O(N) 操作 - 但如果您直接填充 Hashtable,即使这样也可以避免。与以往一样,如果速度是一个问题,请使用秒表计时,看看差异是否显着。
      【解决方案4】:

      终于找到了 NON-Linq 方式

          private static void Main()
          {
              List<tmp> lst = new List<tmp>();
              Dictionary<decimal, string> myDict = new Dictionary<decimal, string>();
              foreach (tmp temp in lst)
              {
                  myDict.Add(temp.Id + temp.Index, string.Format("{0}{1}", temp.Name, temp.LName));
              }
              Hashtable table = new Hashtable(myDict);
          }
      

      【讨论】:

        【解决方案5】:

        作为扩展方法,将List&lt;tmp&gt;转换为Hashtable

        public static class tmpExtensions
            {
            public static System.Collections.Hashtable ToHashTable(this List<tmp> t, bool option)
            {
                if (t.Count < 1)
                    return null;
        
                System.Collections.Hashtable hashTable = new System.Collections.Hashtable();
                if (option)
                {
                    t.ForEach(q => hashTable.Add(q.Id + q.Index,q.Name+q.LName));
                }
                else
                {
                    t.ForEach(q => hashTable.Add(q.Id,q.Index));
                }
                return hashTable;
            }
        }
        

        【讨论】:

          【解决方案6】:

          您可以使用LINQ将列表转换为通用字典,这比原始哈希表要好得多:

          List<tmp> list = GetSomeData();
          var dictionary = list.ToDictionary(entity => entity.Id);
          

          【讨论】:

            【解决方案7】:

            使用 ForEach。

                    List<tmp> lst = GetSomeData();
                    Hashtable myHashTable = new Hashtable();
                    lst.ForEach((item) => myHashTable.Add(item.Id + item.Index, item.Name + item.LName));
            

            【讨论】:

            • 你能给出一个没用的理由吗?
            • 您可以从列表中删除所有项目,将它们一一删除。你也可以打电话给 Clear。你能看出相似之处吗?最好的一段代码是你没有写过的。
            • 添加和删除是完全不同的。但是你有没有看到我们不需要中间结构的字典。如果我们使用字典,我们在字典中添加项目两次,在哈希表中添加第二次。如果数据很高,您不认为这会影响性能吗?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-08-14
            • 1970-01-01
            • 2021-11-26
            • 2012-02-24
            • 2012-02-14
            相关资源
            最近更新 更多