TestModel類定義: public class TestModel{ public int Id { get; set; } public string Name { get; set; } public string Code { get; set; } } Dictionary與List定 ...
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];
}
}