C# 转换列表<;T>;到哈希表

C# 转换列表<;T>;到哈希表,c#,linq,c#-4.0,hashtable,extension-methods,C#,Linq,C# 4.0,Hashtable,Extension Methods,我有一份清单: public class tmp { public int Id; public string Name; public string LName; public decimal Index; } List<tmp> lst = GetSomeData(); 公共类tmp { 公共int Id; 公共字符串名称; 公共字符串名称; 公共十进制索引; } List lst=GetSomeData(); 我想将此列表转换为哈希表,并在

我有一份清单:

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

List<tmp> lst = GetSomeData();
公共类tmp
{
公共int Id;
公共字符串名称;
公共字符串名称;
公共十进制索引;
}
List lst=GetSomeData();
我想将此列表转换为哈希表,并在扩展方法参数中指定
。例如,我可能想要
Key=Id
Value=Index
Key=Id+Index
Value=Name+LName
。我该怎么做

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

var result = new Hashtable(lst.ToDictionary(e=>e.Id, e=>e.Index));
在C#4.0中,您可以使用
字典

但是如果您确实需要一个
哈希表
,请将该字典作为
哈希表
构造函数中的参数传递

var hashTable = new Hashtable(dict);

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

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

您可以使用
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);

您不需要使用来自.NET1.1的
哈希表
字典
更安全。

最后是非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);
    }
private static void Main()
{
List lst=新列表();
Dictionary myDict=新字典();
foreach(第一层的tmp温度)
{
添加(temp.Id+temp.Index,string.Format(“{0}{1}”,temp.Name,temp.LName));
}
哈希表=新哈希表(myDict);
}

作为扩展方法,将
列表
转换为
哈希表

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;
    }
}
公共静态类tmpExtensions
{
public static System.Collections.Hashtable到Hashtable(此列表t,bool选项)
{
如果(t.计数<1)
返回null;
System.Collections.Hashtable Hashtable=新的System.Collections.Hashtable();
如果(选项)
{
t、 ForEach(q=>hashTable.Add(q.Id+q.Index,q.Name+q.LName));
}
其他的
{
t、 ForEach(q=>hashTable.Add(q.Id,q.Index));
}
返回哈希表;
}
}
使用ForEach

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

您是否出于遗留原因使用哈希表(例如,使用某些第三方或旧代码)?如果没有,请使用字典,正如其他评论员所指出的那样。@AppDeveloper这绝对是我第一次看到有人在问题的评语中链接他们的答案。你能给出一个原因吗?你可以从列表中删除所有项目,一个接一个地删除它们。你也可以直接呼叫清除。你能看到相似之处吗?最好的代码是您尚未编写的代码。添加和删除是完全不同的。但你们这里有并没有场景,我们不需要字典,它是中间结构。如果我们使用dictionary,我们将两次添加项,一次在dictionary中,第二次在hashtable中。如果数据非常高,您不认为这会影响性能吗?这很好,但显然会为字典创建额外的数据副本-对于大型数据集,这可能是一个问题。幸运的是,接受IDictionary的哈希表构造函数只是一个O(N)操作——但如果您直接填充哈希表,就可以避免这种情况。和以往一样,如果速度是一个问题,使用秒表计时,看看差异是否显著。
        List<tmp> lst = GetSomeData();
        Hashtable myHashTable = new Hashtable();
        lst.ForEach((item) => myHashTable.Add(item.Id + item.Index, item.Name + item.LName));