我可以将ObservableCollection连接到包含相同类型对象的数组吗?

时间:2014-08-27 20:44:50

标签: c# arrays 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);

关于如何将这两者结合在一起的任何建议?

1 个答案:

答案 0 :(得分:2)

您需要使用docs中所述的Concat()的返回值:

返回值

包含两个输入序列的连接元素的IEnumerable。

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