如何通过Error将对象直接添加到嵌套的可消除的集合对象组 - 无法将列表转换为集合

时间:2017-09-02 09:09:43

标签: c# nested ienumerable generic-list

您好我创建了几个嵌套的iEnumerable实现的类。 CDSWorkflowCollection CDSModuleCollection CDSSystemCollection

以下是我的课程

  public class cdssystems
{
    public string cdsSystemName;
    public CDSModuleCollection listModules;

}


 public class cdsmodules
{
    public string moduleName;
    public CDSWorkflowCollection listWorkflows;
    }
 class cdsdelgate
{
    private string delgateName;
    private DateTime fromDate;
    private DateTime toDate;
    private string functionElement;
    private CDSSystemCollection cdsSystemsList;
    private string cdsComments;
    private string JobTitle;

}

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

}

  class CDSWorkflowCollection : ICollection, IEnumerable<cdsworkflows>
{
    private List<cdsworkflows> cdsWorkflowList;
    private readonly object syncObject = new object();

    public CDSWorkflowCollection(IEnumerable<cdsworkflows> cdsWorkflowList)
        : base()
    {
        this.cdsWorkflowList = new List<cdsworkflows>(cdsWorkflowList);
    }

    public IEnumerator<cdsworkflows> GetEnumerator()
    {
        return this.cdsWorkflowList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.cdsWorkflowList.GetEnumerator();
    }

    public void CopyTo(Array array, int index)
    {
        if ((array.Rank != 1) || ((this.Count + index) > array.Length))
        {
            throw new ArgumentException();
        }

        int count = 0;

        foreach (cdsworkflows cssWorkflow in this.cdsWorkflowList)
        {
            array.SetValue(cssWorkflow, count++);
        }
    }

    public int Count
    {
        get
        {
            return this.cdsWorkflowList.Count;
        }
    }

    public bool IsSynchronized
    {
        get
        {
            return false;
        }
    }

    public object SyncRoot
    {
        get
        {
            return this.syncObject;
        }
    }
}

class CDSSystemCollection : ICollection, IEnumerable<cdssystems>
{
    private List<cdssystems> cdsSystemList;
    private readonly object syncObject = new object();

    public CDSSystemCollection(IEnumerable<cdssystems> cdsSystemList)
        : base()
    {
        this.cdsSystemList = new List<cdssystems>(cdsSystemList);
    }

//这里的其余代码 }

 class CDSDelegateCollection : ICollection, IEnumerable<cdsdelgate>
{
    private List<cdsdelgate> cdsDelegateist;
    private readonly object syncObject = new object();

    public CDSDelegateCollection(IEnumerable<cdsdelgate> cdsDelegateList)
        : base()
    {
        this.cdsDelegateist = new List<cdsdelgate>(cdsDelegateList);
    }

//这里的其余代码 }

不,我想使用group by将类添加到类中,我的代码如下所示

   var 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.FirstOrDefault()["ActionGroup"].ToString()
                                                                                       }                                                                                      
                                                                                       ).ToList()
                                                              }).ToList()


                                        }).ToList();

我在ToList()中遇到错误

无法将类型'System.Collections.Generic.List'隐式转换为'CDS.BusinessObjects.CDSWorkflowCollection'

我知道parm期待一个集合,我正在通过List。如何传递集合。请帮忙

1 个答案:

答案 0 :(得分:0)

删除自定义集合类型,然后使用.net框架中的Generic类型。您可以使用List<T>IList<T>ICollection<T>几乎从不需要编写自己的集合实现。

public class cdsmodules
{
    public string moduleName;
    public CDSWorkflowCollection listWorkflows;
}

成为

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

您也应该遵循正确的可见性指南,不要直接暴露字段。而是使用具有get;set;访问者的属性。字段的范围几乎应始终为privateprotected