0

I need to copy all properties of an object into anonymous object. In JavaScript I'd do it the following way:

items.map(item => ({...item, extra = 'additional property'}))

In C# I would have to copy property by property:

items.Select(item => new { item.Prop1, item.Prop2, Extra = "additional property" })

Or alternatively I'd use reflection and ExpandoObject. Any other thoughts?

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • In my knowledge, nothing built in like that c# for now. Please look into this as well. https://stackoverflow.com/questions/39886700/javascript-spread-syntax-in-c-sharp – zetawars Jul 19 '20 at 12:13
  • 1
    C# 9's prospective records and `with` expressions may interest you: https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#with-expressions – Aluan Haddad Jul 20 '20 at 17:23

0 Answers0