2

I have the following code:

      var contentTypes =
          (
              from contentType in this._contentTypeService.GetContentTypes()
              select new
              {
                  id = contentType.ContentTypeId,
                  name = contentType.Name
              }
          );

How can I add another element to contentTypes with an id of 99 and a name of "All"?

I was trying to use contentTypes.Add(

but that does not seem to be allowed with intellisense.

Csharp
  • 2,916
  • 16
  • 50
  • 77
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • You can convert contentTypes to List and then invoke Add method on it. – Suresh Jun 21 '13 at 16:06
  • 1
    Gemma, you should check for existing Q&A before posting a new question. This has been asked multiple times on StackOverflow, using virtually the same title you used for your question. – Judah Gabriel Himango Jun 21 '13 at 16:08

5 Answers5

2

You can't add to an IEnumerable<T>. IEnumerable<T>s represent sequences that you can iterate over; they do not represent a collection that you can add to. What you can do is concatenate to the end of your sequence though, obtaining a new sequence:

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

Now, if you iterate over sequence, you'll first see the elements of contentTypes streamed to you, and then the final item will be the appended item new { id = 99, name = "All" }.

jason
  • 236,483
  • 35
  • 423
  • 525
1

You can concatenate your new values to the end of the IEnumerable<>.

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"}});

The resulting IEnumerable will end with 99/All

Guy
  • 110
  • 10
0

If you use contentTypes.ToList() you can then add to that list, however doing so will create a new instance of a collection, so your not actually modifying the source collection.

Jay
  • 6,224
  • 4
  • 20
  • 23
0

Try this -

var contentTypes =
          (
              from contentType in this._contentTypeService.GetContentTypes()
              select new
              {
                  id = contentType.ContentTypeId,
                  name = contentType.Name
              }
          ).ToList();

As you have converted contentTypes to a List, it should allow you to add a new item to it.

JustCode
  • 312
  • 1
  • 3
0

First, you cannot use IList.Add on an IEnumerable<T>. So you need to create a new collection.

You are selecting an anonymous type, use Concat to add a fixed anynymous type to your query:

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 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939