当前位置: 移动技术网 > IT编程>开发语言>c# > C# List与Dictionary相互转换与高效查找

C# List与Dictionary相互转换与高效查找

2019年08月02日  | 移动技术网IT编程  | 我要评论

testmodel类定义:

public class testmodel
{
    public int id { get; set; }
    public string name { get; set; }
    public string code { get; set; }

}

 

dictionary与list定义:

list<testmodel> list = new list<testmodel>();
dictionary<int, testmodel> dict = new dictionary<int, testmodel>();

 

dictionary转list:

dict = list.tolookup(model => model.id).todictionary(model => model.key, model => model.first());

 

list转dictionary

list = dict.values.tolist();

 

高效查找:

foreach (testmodel item in list)
{
    if (dict.containskey(item.id))
    {
        testmodel model = dict[item.id];
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网