0

Having this problem solved in JavaScript, I'm trying to do the same using C# for a C# object, not a JSON.

Basically, the JS solving looks like this:

myObject.myObject.forEach(arr => {
 arr.prop = arr.parameters.reduce((res,obj)=> res+obj.special, '')
})

so I tried to do it like:

foreach (array arr in myObject.myObject)
{
    arr.prop = arr.parameters.reduce();
}

I didn't find any function like the JS reduce in C#.

Do you have any suggestions how to solve this?

2
  • reduce == aggregate in .NET Commented Apr 26, 2018 at 14:46
  • 1
    Leo Messi, really? Get life buddy! hold your name proudly. Can you show us your c# code, what you have and what you need to achieve. right now it sounds like, "can you translate this for me?" Commented Apr 26, 2018 at 14:46

1 Answer 1

0

You could do this by first getting all fields or Properties from an object via Reflection

var fields = myObject.GetType().GetFields();

then you pull all current values from those fields and make strings of it

var values = fields.Select(e => e.GetValue(myObject)?.ToString());

and after that, just aggregate the values

var allFieldsInOne = values.Aggregate((e,f) => e + f);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.