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

拥有回忆 提交于 2019-12-11 17:49:29

问题


I have the below classes

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

I am pushing the values to the above Objects from a table with all these data as below. I am getting error in "select new" that:

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

Also if you see the last line ActionGroup = grpWorkflows.Count().ToString() I want to assign the value and not the count. How to do that.

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:


Your model classes contain properties of type List<T> but in your nested queries you do not add ToList() to the projection so it creates an IEnumerable<T> and therefore the error:

For listWorkflows: change to:

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

Same idea for cdsmodules

For the second part of the question of I want to assign the value and not the count change to:

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

Also:

  1. please refer to C# naming conventions: MSDN, dotFactory
  2. I suggest that if you define your fields as public have them as properties. See this for some reference: Properties vs. Fields: Need help grasping the uses of Properties over Fields


来源:https://stackoverflow.com/questions/46007095/cannot-implicitly-convert-type-ienumerableob-to-generic-listcdsworkflows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!