2013-04-02 61 views
22

我有这样代码添加到IEnumerable的

IEnumerable<System.Windows.Documents.FixedPage> page; 

枚举我怎样才能添加页面(例如:d:\ newfile.txt)呢?我试过Add,Append,Concat等但没有为我工作。

+1

你已经尝试过'Concat'?怎么样?您可以将单个页面包装在一个集合中,例如'新[] {singlePage}'。那么你可以使用'Enumerable.Concat':'var pages = page.Concat(new [] {singlePage});' –

+0

非常感谢你 – Sudha

回答

7

是,能够

,能够级联在一起的序列(IEnumerables)和连接后的结果分配给一个新的序列。 (您不能更改原始序列。)

内置的Enumerable.Concat()只会连接另一个序列;然而,编写一个扩展方法很容易,它可以让你将一个标量连接到一个序列。

下面的代码演示:

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Demo 
{ 
    public class Program 
    { 
     [STAThread] 
     private static void Main() 
     { 
      var stringList = new List<string> {"One", "Two", "Three"}; 

      IEnumerable<string> originalSequence = stringList; 

      var newSequence = originalSequence.Concat("Four"); 

      foreach (var text in newSequence) 
      { 
       Console.WriteLine(text); // Prints "One" "Two" "Three" "Four". 
      } 
     } 
    } 

    public static class EnumerableExt 
    { 
     /// <summary>Concatenates a scalar to a sequence.</summary> 
     /// <typeparam name="T">The type of elements in the sequence.</typeparam> 
     /// <param name="sequence">a sequence.</param> 
     /// <param name="item">The scalar item to concatenate to the sequence.</param> 
     /// <returns>A sequence which has the specified item appended to it.</returns> 
     /// <remarks> 
     /// The standard .Net IEnumerable extensions includes a Concat() operator which concatenates a sequence to another sequence. 
     /// However, it does not allow you to concat a scalar to a sequence. This operator provides that ability. 
     /// </remarks> 

     public static IEnumerable<T> Concat<T>(this IEnumerable<T> sequence, T item) 
     { 
      return sequence.Concat(new[] { item }); 
     } 
    } 
} 
+2

是的,它是_possible_,但它**非常低效! –

+2

只是为了澄清,它不会*追加一个项目到现有的集合,它创建一个*新序列*附加了该项目。 – fjdumont

+0

可以修改现有的集合。请参阅下面的答案。 – Steve

8

IEnumerable<T>不包含修改集合的方法。

您将需要实现ICollection<T>IList<T>,因为它们包含添加和删除功能。

5

IEnumerable<T>是一个只读接口。您应该使用IList<T>,它提供了添加和删除项目的方法。

+0

如果你打算使用更具体的接口,我会使用最高级的,你可以拿走,这将是'ICollection '。 – Anthony

1

IEnumerable是不可变的。你不能添加项目,你不能删除项目。
System.Collections.Generic的类将返回此接口,以便您可以遍历集合中包含的项目。

从MSDN

Exposes the enumerator, which supports a simple iteration over a collection of a specified type. 

参见here为MSDN参考。

1

您不能将元素添加到IEnumerable<T>,因为它不支持添加操作。如果可能的话,您必须使用ICollection<T>的实现,或者将IEnumerable<T>转换为ICollection<T>

IEnumerable<System.Windows.Documents.FixedPage> page; 
.... 
ICollection<System.Windows.Documents.FixedPage> pageCollection 
    = (ICollection<System.Windows.Documents.FixedPage>) page 

如果转换是不可能的,使用例如

ICollection<System.Windows.Documents.FixedPage> pageCollection 
    = new List<System.Windows.Documents.FixedPage>(page); 

你可以这样说:

ICollection<System.Windows.Documents.FixedPage> pageCollection 
    = (page as ICollection<System.Windows.Documents.FixedPage>) ?? 
     new List<System.Windows.Documents.FixedPage>(page); 

后者几乎将保证你有一个集合,是修改。不过,使用cast时,可能成功获取集合,但所有修改操作都会抛出NotSupportedException。这对于只读集合是如此。在这种情况下,构造函数的方法是唯一的选择。

ICollection<T>接口实现了IEnumerable<T>,所以你可以使用pageCollection无论你正在使用page

1

尝试

IEnumerable<System.Windows.Documents.FixedPage> page = new List<System.Windows.Documents.FixedPage>(your items list here) 

IList<System.Windows.Documents.FixedPage> page = new List<System.Windows.Documents.FixedPage>(1); 
page.Add(your item Here); 
6

如果您有什么样的原始类型了IEnumerable的是一个想法,你可以修改它...

List<string> stringList = new List<string>(); 
stringList.Add("One"); 
stringList.Add("Two"); 
IEnumerable<string> stringEnumerable = stringList.AsEnumerable(); 
List<string> stringList2 = stringEnumerable as List<string>; 

if (stringList2 != null) 
    stringList2.Add("Three"); 

foreach (var s in stringList) 
    Console.WriteLine(s); 

此输出:

One 
Two 
Three 

更改foreach语句以遍历stringList2stringEnumerable,会得到同样的结果。

反射可能有助于确定IEnumerable的真实类型。

这可能不是一个好习惯,尽管...无论给你什么IEnumerable可能不会期望集合被修改那种方式。

+1

您最好将stringList2转换为ICollection 而不是列表,因为它将适用于列表以及从ICollection继承的其他对象。如果stringList不是ICollection 它将仍然失败并且不会添加。 –