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

时间:2014-04-09 19:18:06

标签: 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<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();  

当我执行代码时,我得到以下错误,这是非常具有描述性的,但是,我似乎无法初始化最后一个&#39; Children&#39;元素到我的生命的空列表。

  

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

如何在底部终止层次结构,以便linq不会抱怨?

0 个答案:

没有答案