如何映射List< Dictionary< enum,string>>列出< object>? [英] How to map a List<Dictionary<enum, string>> to List<object>?

查看:66
本文介绍了如何映射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

推荐答案

一旦尝试这个代码

i创建的enum和类有两个属性



Hi once try this code
i created enum and class which is having two properties

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() }));
           }


我没有一个确切的答案,但建议......你可以很容易地计算 projectstate s使用Linq。



看看这里:

http://stackoverflow.com/questions/16489405/linq-query-with -group-by-and-count-into-anonymous-type [ ^ ]

http://stackoverflow.com/questions/4805826/how-to-write-a-select-count-group- by-sql-query-in-linq [ ^ ]
I've got not an exact answer, but suggestion... You can easily get count of projectstates by using Linq.

Have a look here:
http://stackoverflow.com/questions/16489405/linq-query-with-group-by-and-count-into-anonymous-type[^]
http://stackoverflow.com/questions/4805826/how-to-write-a-select-count-group-by-sql-query-in-linq[^]


这篇关于如何映射List&lt; Dictionary&lt; enum,string&gt;&gt;列出&lt; object&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆