【问题标题】:Cannot implicitly convert type 'IEnumerable<ob>' to 'Generic.List<cdsworkflows>' nested objects无法将类型“IEnumerable<ob>”隐式转换为“Generic.List<cdsworkflows>”嵌套对象
【发布时间】:2017-09-02 04:09:24
【问题描述】:

我有以下课程

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

我正在将所有这些数据的表中的值推送到上述对象,如下所示。我在“选择新”中遇到错误:

不能隐式转换类型 System.Collections.Generic.IEnumerable&lt;cdsworkflows&gt;System.Collections.Generic.List&lt;cdsworkflows&gt;。显式 存在转换(您是否缺少演员表?)

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

【问题讨论】:

  • 你说的是什么意思,我想分配值而不是计数。该怎么做。
  • 实际上我在集合中有 item["ActionGroup"]。这基本上是叶节点。我想将该值分配给 ActionGroup。
  • 查看下面的答案更新

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


【解决方案1】:

您的模型类包含 List&lt;T&gt; 类型的属性,但在您的嵌套查询中,您没有将 ToList() 添加到投影中,因此它会创建一个 IEnumerable&lt;T&gt;,因此会出现错误:

对于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。请参阅此内容以获取一些参考:Properties vs. Fields: Need help grasping the uses of Properties over Fields

【讨论】:

  • 谢谢吉拉德。但是如何在 select 上投影 toList 是否会起作用? (select new cdsworkflows() { WorkflowName = grpWorkflows.Key.ToString(), ActionGroup = grpWorkflows.Count().ToString() }).ToList()
  • 当然这只是一个 POC。是的,一旦开发开始,所有的建议都会做。非常感谢兄弟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多