本文介绍了如何映射List< Dictionary< enum,string>>列出< object>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
List<projectcounterentities> lstResult = new List<projectcounterentities>();
List<dictionary><projectstates,string>> lstProjectCount = new List<dictionary><projectstates,string>>();
lstProjectCount = objClient.GetProjectCounter();
if (lstProjectCount != null)
{
var dicResult = new Dictionary<projectstates,string>();
dicResult = lstProjectCount.ToDictionary();
foreach (var k in dicResult)
{
objProCount = new ProjectCounterEntities();
objProCount.ProjectState = (ProjectStateEnumUI)k.Key;
objProCount.ProjectCount = k.Value;
lstResult.Add(objProCount);
}
return lstResult;
}
我想用respact来检索值,并希望将值和键都映射到DTO列表< projectcounterentities>,其中ProjectCounterEntities是一个DTO和这个DTO的成员是ProjectState和Count。
列表< projectcounterentities> 将是
I want to retrive value with respact to key , and want to map the value and key both into the DTO List<projectcounterentities>, where ProjectCounterEntities is a DTO and the members of this DTO are "ProjectState" and "Count".
The output of List<projectcounterentities>
will be
ProjectState Count
------------------------
projectstate1 2
Projectstate2 5
ProjectState3 6
推荐答案
public enum projectstates
{
projectstate1 = 1,
projectstate2 = 2,
projectstate3 = 3,
projectstate4 = 4
}
public class projectcounterentities
{
public string ProjectState { get; set; }
public string count { get; set; }
}
List<dictionary><projectstates,>> list = new List<dictionary><projectstates,>>()
{
new Dictionary<projectstates,>() { { projectstates.projectstate1, "3" }, { projectstates.projectstate3, "75" } },
new Dictionary<projectstates,>() { { projectstates.projectstate4, "3" }, { projectstates.projectstate2, "15" } }
};
List<projectcounterentities> projectcounterentities = new List<projectcounterentities>();
foreach (var item in list)
{
projectcounterentities.AddRange(item.Select(h => new projectcounterentities() { count = h.Value, ProjectState = h.Key.ToString() }));
}
这篇关于如何映射List< Dictionary< enum,string>>列出< object>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!