2015-03-25 43 views
1

我想使用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] 
+0

爲什麼使用類AttributeObject而不是EF Model類CategoryAttribute? – 2015-03-25 13:01:23

+0

因爲這是在WCF服務中,所以我不想將我的EF模型公開給客戶端。好問題 - 抱歉,我沒有提到帽子。 – JFL 2015-03-25 13:25:17

回答

0

像這樣的東西應該工作:

   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(); 
+0

我收到以下錯誤:「類型'AttributeObject'出現在單個LINQ to Entities查詢中的兩個結構不兼容的初始化中。類型可以在同一個查詢中的兩個位置進行初始化,但只有在兩個位置都設置了相同的屬性地點和這些屬性的設置順序相同。「 – JFL 2015-03-25 14:33:53

+0

對不起, 我會建立一個樣本項目來檢查這個 – 2015-03-25 14:50:12

+0

@JFL我發現了一個工作解決方案,但不能幫助關於非優雅的外觀 注:原文編輯 – 2015-03-25 15:30:55