2014-08-27 108 views
1

我有一個數組和相同類型的對象的ObservableCollection,我想將它們連接在一起。每當我試着連接它們,儘管我只是在ObservableCollection中得到原始數字。我可以將ObservableCollection連接到包含相同類型對象的數組嗎?

public class MyTest { 
     public int id { get; set; } 
     public string Name { get; set; } 
     public bool Registered { get; set; } 
    } 

 ObservableCollection<MyTest> test = new ObservableCollection<MyTest>() 
     { 
      new MyTest(){ 
       id = 1, 
       Name = "Graham", 
       Registered = true 
      }, 
      new MyTest(){ 
       id = 2, 
       Name = "Teresa", 
       Registered = false 
      } 
     }; 

     MyTest[] myTest = { 
      new MyTest(){ 
       id = 3, 
       Name = "Keith", 
       Registered = false 
      }, 
      new MyTest(){ 
       id = 4, 
       Name = "Emilie", 
       Registered = false 
      } 
     }; 

     test.Concat(myTest); 

如何加入這兩個在一起有什麼建議?

回答

2

您需要使用如docs解釋Concat()返回值:

返回值

一個IEnumerable包含兩個輸入序列的拼接元素。

IEnumerable<MyTest> myTests = test.Concat(myTest); 
// 'myTests' variable contains the concatenation of the two collections 
相關問題