我想尝试达到包装在数据模型中的字典的值,但最终我得到的不是我需要的。需要使用 GetAll() 方法一次从所有字典中获取数据;数据模型:
public class Model
{
public Dictionary<int, string> FirstDepartment { get; set; }
public Dictionary<int, string> SecondDepartment { get; set; }
public Dictionary<int, string> ThirdtDepartment { get; set; }
}
管理器或存储库
public class Manager
{
private Model _model;
private List<Model> _lst;
public Manager()
{
_lst = new List<Model>();
_model = new Model()
{
FirstDepartment = new Dictionary<int, string>(),
SecondDepartment = new Dictionary<int, string>(),
ThirdtDepartment = new Dictionary<int, string>()
};
_model.FirstDepartment.Add(1, "Вася");
_model.FirstDepartment.Add(2, "Петя");
_model.SecondDepartment.Add(3, "Катя");
_model.SecondDepartment.Add(4, "Иван");
_lst.Add(_model);
}
public List<Model> GetAll() => _lst;
}
主要的:
static void Main(string[] args)
{
Console.WriteLine("=======================================");
Manager mg = new Manager();
foreach (var item in mg.GetAll())
{
Console.WriteLine($"++++ {item} ++++");
}
Console.WriteLine();
Console.ReadKey();
}
我得到以下信息:
怎样才能正确打包,然后遍历整个模型的所有值(所有字典的Values)。

字典值连接:
考试
结论