views:

91

answers:

1

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?

A: 

You can use the Concat method:

var q = new[]{ new { ID = null, Name = "All" } }.Concat(dbQuery);
Mark Seemann
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