views:

98

answers:

5

I have a collection of objects to which I'd like to just add a new property. How do I do that with LINQ?

+1  A: 
var a = from i in ObjectCollection select new {i.prop1, i.prop2, i.prop3, ..., newprop = newProperty}
John Boker
A: 

@John:

Yes, that's what I came up with too, but I was wondering if there's a way to do it without having to remap all the properties. All I'd really like to do is just "add" the new one.

Esteban Araya
A: 

I don't think that you can using pure LINQ. However, if you're doing this sort of thing a lot in your code you may be able to make this work with reflection.

Guy
A: 

@Esteban yeah, i know what you mean but i dont know of another way either, maybe someone else reading this will.

John Boker
A: 

Why do you want to add the extra property? Or, put a different way, what do you intend to do with the property once you have it in your new IEnumerable source?

If you need it for data binding, I have a helper class that might help you.

Lasse V. Karlsen
I'd be interested in seeing this helper class.
jfar