4

I'm wondering what the best approach to adding an artificial row to an anonymous linq result set would be.

I have a linq statement which uses "select new" to form the data required. Each record comes back as an anonymous object with ID and Name properties. However, I require that the first row of the data becomes an object with ID = NULL, Name="All".

Is there a way to union in an artificial result into the Linq query? Or else, how do I add a new instance of the anonymous type into the anonymous result collection?

Brian Scott
  • 9,221
  • 6
  • 47
  • 68

1 Answers1

3

You can use the Concat method:

var q = new[]{ new { ID = null, Name = "All" } }.Concat(dbQuery);
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 2
    Hi thanks, I'd just come back to post a similiar solution once I worked it out and seen your post. The thing I was missing was that I hadn't realised I could simply create a new anonymous object instance wih the named Parameters syntax so long as the properties matched. I ended up using the following to get my row inserted as the first item but the outcomes the same: anonCollection.Insert(0, new { Name = "All", ID = "" }); Thanks. – Brian Scott Feb 11 '10 at 10:31