-1

This code which initializes an array with two hard-coded values is working perfectly fine:

var db = new GoogleGraph {
    cols = new ColInfo[] {
        new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
        new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }                       
    }.ToList(),
    rows = new List<DataPointSet>()
};
db.cols.AddRange(listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" }));

This code which attempts to add some dynamically generated values is not working:

var db = new GoogleGraph {
    cols = new ColInfo[] {
        new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
        new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" },
        listOfValues.Select(p => new ColInfo { id = "", label = p, type = "number" })                     
    }.ToList(),
    rows = new List<DataPointSet>()
};

How can I correctly implement the above snippet?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Seehyung Lee
  • 590
  • 1
  • 15
  • 32

1 Answers1

1

You can't pass an IEnumerable<T> to an initializer of T[] like that.

You can do what you want by putting the hard-coded objects in their own collection, then concatenating the dynamic ones:

var db = new GoogleGraph {
    cols = 
        new ColInfo[] {
            new ColInfo { id = "", label = "Date", pattern ="", type = "string" },
            new ColInfo { id = "", label = "Attendees", pattern ="", type = "number" }
        }
        .Concat(listOfValues.Select(p => 
            new ColInfo { id = "", label = p, type = "number" }))                     
        .ToList(),
    rows = new List<DataPointSet>()
};
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272