无法隐式转换类型&#39; IEnumerable <ob>&#39;到&#39; Generic.List <cdsworkflows>&#39;嵌套对象

时间:2017-09-01 19:47:31

标签: c# asp.net linq group-by generic-list

我有以下课程

public class cdsworkflows
{
    public string WorkflowName;
    public string ActionGroup;
}  

public class cdssystems
{
    public string cdsSystemName;
    public List<cdsmodules> listModules;
}

public class cdsmodules
{
    public string moduleName;
    public List<cdsworkflows> listWorkflows;
}

我正在从包含所有这些数据的表中将值推送到上述对象,如下所示。我在&#34;选择新的&#34;的是:

  

无法隐式转换类型   System.Collections.Generic.IEnumerable<cdsworkflows>来   System.Collections.Generic.List<cdsworkflows>。一个明确的   存在转换(你错过了演员吗?)

另外,如果您看到最后一行ActionGroup = grpWorkflows.Count().ToString(),我想分配值而不是计数。怎么做。

List<cdssystems> results =
    (from SPListItem item in myItemsList
     group item by item["Systems"]
     into grp
     select new cdssystems()
     {
         cdsSystemName = grp.Key.ToString(),
         listModules =
            from item in grp
            group item by item["Modules"]
            into grpModules
            select new cdsmodules()
            {
                moduleName = grpModules.Key.ToString(),
                listWorkflows =
                    from item in grpModules
                    group item by item["Workflows"]
                    into grpWorkflows
                    select new cdsworkflows()
                    {
                        WorkflowName = grpWorkflows.Key.ToString(),
                        ActionGroup = grpWorkflows.Count().ToString()
                    }
            }
     }
    ).ToList();

1 个答案:

答案 0 :(得分:0)

您的模型类包含List<T>类型的属性,但在嵌套查询中,您不会将ToList()添加到投影中,因此会创建IEnumerable<T>因此错误:

对于listWorkflows:更改为:

listWorkflows = (from item in grpModules
                 group item by item["Workflows"] into grpWorkflows
                 select new cdsworkflows
                 {
                    WorkflowName = grpWorkflows.Key.ToString(),
                    ActionGroup = grpWorkflows.Count().ToString()
                 }).ToList()

cdsmodules的相同想法

对于问题的第二部分,我想将值而不是计数更改为:

 ActionGroup = string.Join(", ", grpWorkflows.Select(i => i["ActionGroup"]))

此外:

  1. 请参阅C#命名约定:MSDNdotFactory
  2. 我建议,如果您将字段定义为public,则将其设为properties。请参阅此内容以获取参考:Properties vs. Fields: Need help grasping the uses of Properties over Fields