Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Linq创建嵌套结构会引发错误_Linq_Linq To Entities - Fatal编程技术网

使用Linq创建嵌套结构会引发错误

使用Linq创建嵌套结构会引发错误,linq,linq-to-entities,Linq,Linq To Entities,我想执行一个查询,创建一组层次结构。这个查询看起来像 public class PrimaveraWBS { public int PrimaveraId { get; set; } public int InternalId { get; set; } public int ContainerId { get; set; } public string ContainerName { get; set; } public IEnumerable<

我想执行一个查询,创建一组层次结构。这个查询看起来像

public class PrimaveraWBS
{
    public int PrimaveraId { get; set; }
    public int InternalId { get; set; }
    public int ContainerId { get; set; }

    public string ContainerName { get; set; }
    public IEnumerable<PrimaveraWBS> Children { get; set; }
}

var wbscodes = (from o in Projects
                where o.Id == id
                from p in o.ProjectDisciplines
                select new PrimaveraWBS
                {
                    ContainerName = p.Discipline.Name,
                    ContainerId = p.Id,
                    PrimaveraId = 0,
                    Children = (from q in CheckLists
                        where q.Assessment_Id == lastAssessmentId && q.Discipline.Id == p.Discipline_Id
                        select new PrimaveraWBS
                        {
                            ContainerName = q.Name,
                            ContainerId = q.Id,
                            PrimaveraId = 0,
                            Children = (from r in q.Groups
                                select new PrimaveraWBS
                                {
                                    ContainerName = r.Name,
                                    ContainerId = r.Id,
                                    PrimaveraId = 0,
                                }).ToList()
                        }).ToList()
                }).ToList();  
当我执行代码时,我得到了以下错误,这是非常描述性的,但是,我似乎无法在我的一生中将最后一个“Children”元素初始化为空列表

NotSupportedException:类型“STOrm.Utility.PrimaveraWBS”在单个LINQ-to-Entities查询中出现在两个结构不兼容的初始化中。一个类型可以在同一查询中的两个位置初始化,但前提是在两个位置设置了相同的属性,并且这些属性的设置顺序相同


我怎样才能终止底部的层次结构,这样linq就不会抱怨了?

仅供参考,因为我尝试了不同的事情,所以所有的收费者都在那里。它们通常不会出现在查询中。我没有遇到过这种情况,但您是否尝试在最内部的PrimaveraWBS属性初始值设定项中显式设置Children=null?是的,我有,但这不起作用。它会引发另一个错误。很奇怪,这似乎是一个如此简单的要求