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
2008-09-05 16:19:35
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
2008-09-05 16:24:29
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
2008-09-05 16:29:37
A:
@Esteban yeah, i know what you mean but i dont know of another way either, maybe someone else reading this will.
John Boker
2008-09-05 18:39:17
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
2008-09-05 18:43:26
I'd be interested in seeing this helper class.
jfar
2008-12-17 14:54:40