只有数据量大时才会有效果(几万条以上)
C#list循环时由于数据量太大,循环起来太慢了。
把list转成Dictionary(字典)后循环,效率会提升很快。
实体
public class Model
{
public string ID { get; set; }
public string Name { get; set; }
public int Level { get; set; }
}
list -> Dictionary -> for循环找到传入key值model
List<Model> list = new List<Model>();
Dictionary<string, Model> listDic = list.ToDictionary(t => t.ID);
var model = new Model();
if (listDic .Keys.Contains(key))
model = listDic [key];
还有更快的C#循环方法吗