标签:高效 contain contains class list() value 定义 nta str
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];
}
}
标签:高效 contain contains class list() value 定义 nta str
原文地址:https://www.cnblogs.com/s0611163/p/11289076.html