2017-05-25 67 views
0

我需要在樹上表示數據,每個節點有id,textattributesLINQ to Entities - 初始化複雜對象(Dictionary或List <KeyValuePair>)

我試過讓我的attributes屬性通用,並使用LINQ to Entities進行初始化。


嘗試1

隨着我的課爲:

public class TreeNode 
{ 
    public int id { get; set; } 
    public string text { get; set; } 
    public Dictionary<string, object> attributes { get; set; } 
    public int? parent { get; set; } 
} 

我試着初始化:

List<TreeNode> taskItems = new List<TreeNode>(); 
using (var database = new MyDatabase()) 
{ 
    taskItems = 
    (
     from t in database.Task 
     select new TreeNode() 
     { 
      id = t.ID, 
      text = t.Name, 
      attributes = new Dictionary<string, object>() 
      { 
       { "Color", t.Color }, 
       { "Category", t.Category } 
      }, 
      parent = t.ParentID 
     } 
    ) 
    .ToList(); 
} 

上述失敗,出現錯誤

LINQ to Entities支持只列出具有單個元素的初始化程序項目。


嘗試2

我試圖List<KeyValuePair>代替Dictionary

public class TreeNode 
{ 
    public int id { get; set; } 
    public string text { get; set; } 
    public List<KeyValuePair<string, object>> attributes { get; set; } 
    public int? parent { get; set; } 
} 

我試圖初始化:

List<TreeNode> taskItems = new List<TreeNode>(); 
using (var database = new MyDatabase()) 
{ 
    taskItems = 
    (
     from t in database.Task 
     select new TreeNode() 
     { 
      id = t.ID, 
      text = t.Name, 
      attributes = new List<KeyValuePair<string, object>>() 
      { 
       new KeyValuePair<string, object>("Color", t.Color), 
       new KeyValuePair<string, object>("Category", t.Category) 
      }, 
      parent = t.ParentID 
     } 
    ) 
    .ToList(); 
} 

這一次上述失敗,錯誤

只有參數構造函數初始化和LINQ中支持到實體。


我如何能實現attributes屬性的初始化?

+0

[本SO](https://stackoverflow.com/questions/35014278/how-to-create-a-dictionary-in-linq -when-populating-a-class/35014914)應該對你有所幫助。 –

回答

2
database.Task 
.Select(t=> new 
{ 
      t.ID, 
      t.Name, 
      t.Color, 
      t.Category, 
      t.ParentID 
}).AsEnumerable() 
.Select(t=> new TreeNode 
     { 
      id = t.ID, 
      text = t.Name, 
      attributes = new List<KeyValuePair<string, object>> 
      { 
       new KeyValuePair<string, object>("Color", t.Color), 
       new KeyValuePair<string, object>("Category", t.Category) 
      }, 
      parent = t.ParentID 
     }).ToList() 
+0

這工作。我已經發布了我的工作代碼作爲語法替代。 – SNag

+0

問題是:如果在這種情況下只有兩個類型屬性是顏色和類別,定義List的目的是什麼?只需簡單地將兩個屬性定義爲像我一樣的顏色和類別。我不明白你爲什麼使用字典或列表。 – ArgeKumandan

+0

上面的代碼是我的生產代碼的簡化版本,它有更多的屬性屬性。 – SNag

2

KeyValuePair是不可變的。創建實例後,您無法更改其值。 創建自定義類:

class KeyValuePairCustom<T,S> 
{ 
    public KeyValuePairCustom() {} 

    public T Key {get;set;} 
    public S Value {get;set;} 
} 

比你的LINQ語句中使用下面的代碼:

List<TreeNode> taskItems = new List<TreeNode>(); 
using (var database = new MyDatabase()) 
{ 
    taskItems = 
    (
     from t in database.Task 
     select new TreeNode() 
     { 
      id = t.ID, 
      text = t.Name, 
      attributes = new List<KeyValuePairCustom<string, object>>() 
      { 
       new KeyValuePairCustom<string, object>() { 
        Key="Color", 
        Value = t.Color 
       }, 
       new KeyValuePair<string, object>() { 
        Key = "Category", 
        Value = t.Category 
       } 
      }, 
      parent = t.ParentID 
     } 
    ) 
    .ToList(); 
} 
2

@ArgeKumandan的解決方案工作。

這是一種語法的替代,並且使用Dictionary

List<TreeNode> taskItems = new List<TreeNode>(); 
using (var database = new MyDatabase()) 
{ 
    taskItems = 
    (
     from t in database.Task 
     select new 
     { 
      id = t.ID, 
      text = t.Name, 
      color = t.Color, 
      category = t.Category, 
      parent = t.ParentID 
     } 
    ) 
    .AsEnumerable() 
    .Select(t => new TreeNode() 
    { 
     id = t.id, 
     text = t.text, 
     attributes = new Dictionary<string, object>() 
     { 
      { "color", t.color }, 
      { "category", t.category } 
     }, 
     parent = t.parent 
    }) 
    .ToList(); 
}