Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中向IEnumerable添加元素#_C#_Ienumerable - Fatal编程技术网

C# 如何在C中向IEnumerable添加元素#

C# 如何在C中向IEnumerable添加元素#,c#,ienumerable,C#,Ienumerable,我有以下代码: var contentTypes = ( from contentType in this._contentTypeService.GetContentTypes() select new { id = contentType.ContentTypeId, name = contentType.

我有以下代码:

      var contentTypes =
          (
              from contentType in this._contentTypeService.GetContentTypes()
              select new
              {
                  id = contentType.ContentTypeId,
                  name = contentType.Name
              }
          );
如何向contentTypes添加另一个id为99、名称为“All”的元素

我试图使用contentTypes。添加(


但intellisense似乎不允许这样做。

如果使用
contentTypes.ToList()
,则可以添加到该列表中,但这样做将创建一个新的集合实例,因此您不会实际修改源集合。

尝试以下操作-

var contentTypes =
          (
              from contentType in this._contentTypeService.GetContentTypes()
              select new
              {
                  id = contentType.ContentTypeId,
                  name = contentType.Name
              }
          ).ToList();
当您已将contentTypes转换为列表时,它应该允许您向其中添加新项。

您不能添加到。
IEnumerable
s表示您可以迭代的序列;它们不表示您可以添加到的集合。但是,您可以做的是在序列的末尾,获取一个新序列:

var sequence = contentTypes.Concat(
                   new[] {
                       new { id = 99, name = "All" }
                   }
               );

现在,如果您迭代
序列
,您将首先看到流式传输给您的
contentTypes
元素,然后最后一项将是附加项
new{id=99,name=“All”}

首先,您不能在
IEnumerable上使用
IList.Add
。因此您需要创建一个新集合

如果要选择匿名类型,请使用
Concat
将固定的任意名称类型添加到查询中:

var allTypes = new[]{new { id = 99, name = "All" }};    // creates a fixed anonymous type as `IEnumerable<T>`
var contentTypes = from contentType in this._contentTypeService.GetContentTypes()
                   select new
                   {
                       id = contentType.ContentTypeId,
                       name = contentType.Name
                   };
var result = allTypes.Concat(contentTypes).ToList(); // concat 
var allTypes=new[]{new{id=99,name=“All”};//创建一个固定的匿名类型作为`IEnumerable`
var contentTypes=来自此中的contentType.\u contentTypeService.GetContentTypes()
选择新的
{
id=contentType.ContentTypeId,
name=contentType.name
};
var result=allTypes.Concat(contentTypes.ToList();//Concat

您可以将新值连接到IEnumerable的末尾


生成的IEnumerable将以99/All结尾

您可以将contentTypes转换为List,然后在其上调用Add方法。Gemma,在发布新问题之前,您应该检查现有的Q&A。这在StackOverflow上已被多次询问,使用的标题与您在问题中使用的标题几乎相同。
var contentTypes =
   (
      from contentType in new[]{new {ContentTypeId = 1, Name="TEST"}}
      select new
      {
          id = contentType.ContentTypeId,
          name = contentType.Name
      }
   ).Concat(new[]{new {id = 99, name="All"}});