代码之家  ›  专栏  ›  技术社区  ›  Lance Rushing

C#concat two Collection<string>使用linq并获得一个Collection<string>结果

  •  5
  • Lance Rushing  · 技术社区  · 14 年前

    我试着这么做:

    var collection1 = new Collection<string> {"one", "two"};
    var collection2 = new Collection<string> {"three", "four"};
    
    var result = collection1.Concat(collection2);
    

    但是结果变量的类型是可枚举的[系统字符串]

    var all = (Collection<string>) collection1.Concat(collection2);
    

    2 回复  |  直到 14 年前
        1
  •  14
  •   James Curran    14 年前
    var result = new Collection<string>(collection1.Concat(collection2).ToList());
    

    IEnumerator )

        2
  •  4
  •   Paul Ruane    10 年前

    使用 Enumerable.ToList() ,作为 List<> ICollection<> .

    IList list = a.Concat(b).ToList()
    

    如果你是说 System.ObjectModel.Collection<> 然后必须将创建的列表传递到 Collection<> 我知道不太理想。

    var collection = new System.ObjectModel.Collection<string>(a.Concat(b).ToList());