Linq to EF - 从 EDM C# 填充一个(相当简单的)分层 object

Linq to EF - populate a (fairly simple) hierarchical object from EDM C#

我想使用 linq to EF 填充 parent object 及其 children 的列表(第一级 children)。 (最终)方法将采用 parent 输入和 return 项目及其所有 children。我 posted 的示例使用 parented==null 来获取 top-level 项目。只是想让它先工作。

我正在 post创建 table 语句,示例数据位于此 post 的末尾。

我要填充的 object 称为 AttributeObject。它包含一个名称(即数据库中的 "Value" 列)及其 children.

的列表
    public class AttributeObject
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public List<AttributeObject> Children { get; set; }
    }

我正在努力加载 children。我的代码是:

        var attributes =
            (from attr in model.CategoryAttributes.Include(c => c.Children)
             where attr.ParentID == null
             select new AttributeObject()
             {
                 Name = attr.Value,
                 ID = attr.ID,
                 Children = attr.Children.AsEnumerable().ToList<AttributeObject>()
             });

我得到的错误是它无法从 CategoryAttribute(实体类型)转换为 AttributeObject

非常感谢

SQL Table:

    CREATE TABLE [CategoryAttribute](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Value] [varchar](1000) NOT NULL,
        [ParentID] [int] NULL,
        [Weight] [int] NULL,
     CONSTRAINT [PK_ECM_CategoryAttributeValues] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
    ) ON [PRIMARY]

    SET IDENTITY_INSERT [CategoryAttribute] ON 

    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (1, N'Document Properties', NULL, 1)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (2, N'Document Group', 1, 1)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (3, N'Document Type', 2, 1)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (12, N'IS Agreement', 2, 5)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (13, N'IS Guides', 2, 5)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (70, N'Service Agreement', 12, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (71, N'Software Licensing', 12, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (72, N'SOW', 12, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (73, N'Support Contracts', 12, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (74, N'Administration Guide', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (75, N'Developer Guide', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (76, N'Installation Guide', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (77, N'Procedure', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (78, N'Release Notes', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (79, N'Training Manual', 13, 8)
    INSERT [CategoryAttribute] ([ID], [Value], [ParentID], [Weight]) VALUES (80, N'User Guide', 13, 8)
    SET IDENTITY_INSERT [CategoryAttribute] OFF
    ALTER TABLE [CategoryAttribute]  WITH CHECK ADD  CONSTRAINT [FK_ECM_CategoryAttributeValues_ECM_CategoryAttributeValues] FOREIGN KEY([ParentID])
    REFERENCES [CategoryAttribute] ([ID])
    ALTER TABLE [CategoryAttribute] CHECK CONSTRAINT [FK_ECM_CategoryAttributeValues_ECM_CategoryAttributeValues]

像这样的东西应该可以工作:

             var attributes =
                                (from attr in model.CategoryAttributes.Include(c => c.Children)
                                 where attr.ParentID == null
//Edited Here
                                 select new
            {
                Name = attr.Value,
                ID = attr.ID,
                Children = attr.Children
                .Select(c => new AttributeObject() { Name = c.Value, ID = c.ID })
                        .ToList()
            })
   //and Here execute the query with LINQ to Entities
    .AsEnumerable()
   //correctly cast to AttributeObject with LINQ to Object
                .Select(n => 
                    new AttributeObject() {
                Name=n.Name,
                ID=n.ID,
                Children=n.Children

            }).ToList();