我想使用linq將一個父對象及其子項(一級子項)填充到EF。 (最終)方法將採用父輸入並返回項目及其所有子項。我發佈的示例使用parented == null來獲取頂級項目。試圖讓它首先工作。Linq to EF - 從EDM C填充一個(相當簡單)的分層對象#
我發佈了創建表語句,並在本文末尾添加了示例數據。
我想要填充的對象稱爲AttributeObject。它包含一個名稱(這是數據庫中的「值」列)以及它的子項列表。
public class AttributeObject
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int ID { get; set; }
[DataMember]
public List<AttributeObject> Children { get; set; }
}
我正在努力加載孩子。我的代碼是:
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表:
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]
爲什麼使用類AttributeObject而不是EF Model類CategoryAttribute? – 2015-03-25 13:01:23
因爲這是在WCF服務中,所以我不想將我的EF模型公開給客戶端。好問題 - 抱歉,我沒有提到帽子。 – JFL 2015-03-25 13:25:17