Combine various properties of a C# object with different lengths into a single new property within the same object

In search of a solution similar to what was provided for JavaScript in this thread, I am now attempting to achieve the same outcome using C# with a C# object, rather than JSON.

The JavaScript resolution appears as follows:

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

Hence, I tried to replicate it like so in C#:

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

Unfortunately, I have not been able to locate an equivalent function such as JS reduce in C#.

Are there any suggestions on how to tackle this issue?

Answer №1

One way to accomplish this is by utilizing Reflection to retrieve all fields or properties from an object.

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

Next, extract the current values of these fields and convert them into strings.

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

Finally, combine all the values together through aggregation.

var allFieldsInOne = values.Aggregate((e,f) => e + f);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to retrieve directory information in Next.js hosted on Netlify

Having trouble retrieving a list of directories in Next.js on Netlify. The code works fine on localhost, but once deployed to Netlify, an error is triggered: { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": ...

Stale Reference Exception in WebElements List encountered with Selenium WebDriver

On a Selenium page, I have defined a list as a property: public IEnumerable<MyItem> MyList => webDriverInstance.FindListElementsWithWait<MyItem>(By.ClassName("my-item")); The FindListElementsWithWait method is an extension method: ...

The method of inserting a JSON dates object to deactivate specific days

I am currently utilizing a date picker component that can be found at the following link: While attempting to use the disabledDays section below, I have encountered an issue where I am unable to apply all three options. The blockedDatesData option works o ...

.NET Application that compiles code for Windows forms

I'm encountering an issue. I'm struggling to locate a mechanism in .Net that enables me to compile code within a Windows form. I would like to have a single textbox where I can input my code and have the compiled code displayed in another box. Ho ...

Create a function in JavaScript that is able to accept a variable number of objects as arguments

I have a good grasp of how to pass infinite parameters in a function in JavaScript. But what about accepting any number of objects as parameters in a function? This is my current implementation: function merge<T>(objA: T, objB: T){ return Object. ...

Discover the best way to reference a JavaScript variable within an HTML form textfield

I'm having trouble with a script that is supposed to display the selected value from a drop down list in a text field on an HTML form. When I select an option, the value is not appearing in the text field. Can someone please assist me with this issue? ...

I am having an issue in React.js where my state is not updating until the second click instead of the first one

Whenever I click items on my dropdown menu, the state only updates after clicking twice. I have provided a video and some code snippets for reference. How can I resolve this issue? https://i.sstatic.net/LkXUx.gif Parent Class Component (APP): class App e ...

Manipulate objects within an array by updating state with a button in React

So, I have an array of names that I want to cycle through: const data = { names: [ { name: "Jordan" // additional data }, { name: "Holly" // additional data }, { name: "Sean" // additional data ...

Building a game using Javascript and a database for a project

I am currently working on developing an online game using a combination of javascript and php. My main objective is to create a seamless, real-time gameplay experience without the need to constantly refresh the page. However, I have encountered a signific ...

Developing a Node.js API using Express and MySQL involves utilizing the WHERE IN clause and binding parameters with multiple comma-separated values

Having a URL structure as shown below, where multiple comma-separated values can be added to the URL: localhost:4001/api/v1/users/search?title=mr,dr This is my query implementation: router.get('/search?', function(req, res, next) { var ...

Transforming various arrays into the insert_batch style of CodeIgniter

I'm working on creating a multi-lingual form using CodeIgniter. Here is the structure of my POST array: Array ( [title] => Array ( [en] => English Title [de] => German Title ) [url] => Array ( [en ...

Combining td elements within a table

Currently, I am working on creating a weekly calendar using a combination of JavaScript and PHP to interact with an SQL table. The process involves generating an empty table structure in JavaScript and then populating specific cells with data retrieved fro ...

Converting loosely typed BsonDocument to JSON format within a controller

I am facing an issue with the MongoDB driver in my .Net Core web api that is connected to a JavaScript frontend. The setup involves a Parent and Child class. I wanted to create a weakly typed field in the Child class where I could store any JSON value as a ...

Exploring Operator Referencing in Java

I am struggling to figure out the operator that can replace the return statement, allowing me to simply pass the operator itself and bypass the return statement entirely. Having experience in C#, I have realized that the following code: public IWebelemen ...

Getting Started with Parsing JSON Objects in ReactJS

In my ReactJS project, I am trying to parse through a JSON list using the following model: public class ModelEmployee { public List<Employeelist> Employees { get; set; } public int count { get; set; } public int Pagenumber { get; set; } ...

A guide on creating ProcessStartInfo for saving to a text file in a Linux environment

I am currently using ProcessStartInfo in my .NET application to run a Linux shell script (".sh" file). I have managed to successfully copy the script's contents to a text file on a Windows environment, however, the same process is not working on Linux ...

Building interactive forms in Angular 6

I want to design a dynamic form that creates a new form when the AddNew button is clicked. In my TypeScript (ts) file, I have the following code: addinfoForm: FormGroup; infoNameList: FormArray; infoModel: Productdetail; constructor(private fb ...

Accessing JS code from HTML is not possible in React

Attempting to create a list using React, I have utilized the module found here: https://github.com/pqx/react-ui-tree I am currently testing out the sample application provided in this link: https://github.com/pqx/react-ui-tree/blob/gh-pages/example/app.js ...

Can you explain the distinction between utilizing Object.create(BaseObject) and incorporating util.inherits(MyObject, BaseObject)?

Can you explain the difference between these two methods of setting the prototype? MyObject.prototype = Object.create(EventEmitter.prototype); MyObject.prototype = util.inherits(MyObject, EventEmitter); UPDATE I've noticed in various projects that ...

Dynamic element not firing jQuery event

My question is...https://i.sstatic.net/me2uQ.png // Using ajax to dynamically create a table $(".n").click(function(){ var id= $(this).closest('tr').find('td.ide2').html(); //for displaying the table $.ajax({ ...