无法将类型 'IEnumerable<ob>' 隐式转换为 'Generic.List<cdsworkflows>' 嵌套对象

Cannot implicitly convert type 'IEnumerable<ob>' to 'Generic.List<cdsworkflows>' nested objects

我有下面的类

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

我正在将值从 table 推送到上述对象,所有这些数据如下。我在 "select new" 中收到错误:

Cannot implicitly convert type System.Collections.Generic.IEnumerable<cdsworkflows> to System.Collections.Generic.List<cdsworkflows>. An explicit conversion exists (are you missing a cast?)

此外,如果您看到最后一行 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();

您的模型 类 包含类型 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#命名规则:MSDN, dotFactory
  2. 我建议,如果您将字段定义为 public,则将它们定义为 properties. See this for some reference: Properties vs. Fields: Need help grasping the uses of Properties over Fields