Transforming a Datatable into an array using C#

Looking to convert a DataTable into a 2-D array in C#. Here's an example for better understanding.

DataTable in C#:

code           Price
----------     ----------
1146441600000  34
1146528000000  5
1146614400000  10
1146700800000  7
1146787200000  12
1147046400000  8
1147132800000  9

Desired Output in JavaScript:

[[1146441600000,34],
[1146528000000,5],
[1146614400000,10],
[1146700800000,7],
[1146787200000,12],
[1147046400000,8],
[1147132800000,9]]

Razor engine is being used for front-end rendering.

Answer №1

To achieve this, you can utilize format options along with Linq extension functions in the following manner.

DataTable sourceData; // assign your data source
var selectedRows = sourceData.AsEnumerable()
             .Select(row => string.Format("[{0}]", string.Join(",", row.ItemArray)));

var result = string.Format("[{0}]", string.Join(",", selectedRows.ToArray()));

For a demonstration, you can refer to this Demo

Answer №2

Super simple:

Let newArray = tableData.Rows.Cast<DataRow>().Select(r => r[0].ToString()).ToArray();

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

Use AJAX to send the values selected in two dropdown menus to a PHP script without the need to click

I am currently working on a project that involves two dropdown lists. I need to capture the user's selection from these dropdowns in order to fetch data from a database using PHP. The challenge is that there is no submit button, and I am unsure of how ...

Creating a multidimensional array or array tree from a list of arrays - A step-by-step guide!

My challenge involves working with an array that looks like this: ["Lorem", "Ipsum", "Colo", "sit", "ame", "consecteur"] The goal is to create subarrays with a combined character length of 10, resulting in something like this: [ ["Lorem", "Ipsum"], ...

Traversing Through a Complicated JSON Structure

An application I developed can accept faxes in XML format and convert them into JSON objects to extract the necessary information, specifically the base64 string within the "file contents" variable of the document. Here is the code snippet: exports.recei ...

Exploring the implementation of the href tag in ASP.NET

I'm diving into the world of ASP.NET and I have a question about my code. In my content file, there is a line that reads: <a href="products/myproduct"> Now, I have a view file named Myproduct.aspx and within the ProductsController there is a m ...

PHP loop exceeding the allocated memory limit

I am encountering a critical error when attempting to process a large array of arrays in PHP and return the outcome as a response to an HTTP POST request: Memory limit of 536870912 bytes exceeded I have attempted to address this by setting ini_set(&apo ...

Checking a condition with a for loop in Javascript

I'm working on developing a function that can generate a random number between 0 and 9 that is not already included in an array. Here is the code I have come up with so far: var myArr = [0,2,3,4]; console.log("Array: " + myArr); function newN ...

Refresh the JSON data using JavaScript

Looking to dynamically update data every 5 seconds using jQuery? Check out the URL for the JSON data here. Below is the code snippet that I am experimenting with: $.getJSON("http://gdx.mlb.com/components/game/win/year_2015/month_11/day_11/master_scoreboa ...

Attempting to retrieve and substitute the final value within an input field by cross-referencing or evaluating it against a different value

Can you help me with a solution for fetching the last value (word or character) in an input box, storing it in a variable, and then replacing it with one of a set of values that match exactly or partially? The items within the bindname-block contain vario ...

Guide on dynamically assigning the value of an Angular variable to another variable

My website has a slideshow feature with left and right buttons, similar to this example: . I am using Angular to change the image when the left or right button is clicked. In the function, I am incrementing a value: /*SlideShow Pictures*/ $scope.pic ...

Is there a more efficient method for repeatedly writing a for loop?

I'm currently striving to enhance my Java programming skills. I am a novice and have been practicing writing basic programs and data structures in my spare time before beginning college next year! Is there a more efficient way to optimize the code be ...

Adjusting the height of each suggested item in Jquery autocomplete

I am currently facing an issue with the autocomplete input on my web app. The suggested list items have a height that is too small, making it difficult to select one item on a tablet using fingers. .ui-autocomplete.ui-widget { font-family: Verdana, Arial, ...

A step-by-step guide on implementing the bliss view engine in Express.js instead of Jade

Is there a way to utilize the bliss view engine instead of the typical jade engine in Express JS? I came across an article on Stack Overflow, but it seems to be geared towards an older version of express.js. I am using version 3.x. Specifically, I am int ...

Using jQuery to iterate through rendered HTML with the ForEach function

I am utilizing JS/jQuery code to extract the cell value of an ASP DetailsView control (rendered HTML), validate it against a condition, and hide a specific div based on the result. Specifically, the code is examining whether the cell value is formatted lik ...

Ways to utilize an array within a function to specifically retrieve the desired value

I am struggling to integrate this function into my WordPress theme in a way that allows me to retrieve only the specific values I need, rather than all of them together. I am not very familiar with using arrays in functions, so I would greatly appreciate y ...

In TypeScript, use a Record<string, any> to convert to {name: string}

I have developed a custom react hook to handle API calls: const useFetch: (string) => Record<string, any> | null = (path: string) => { const [data, setData] = useState<Record<string, any> | null>(null); var requestOptions: Requ ...

Struggling to Retrieve Specific Keys for Individual Values in Firebase with React Native

I am currently experiencing difficulty obtaining a unique key for each value in the 'users1' table. firebase.database().ref('users1').once('value').then(snapshot => { var items = []; snapshot.forEach((child) => { ...

Managing a unified JSON array to store data for 5 specific dropdown fields using JavaScript

Is there a way to populate a JSON array like [{k1:"v1"},{k2:"v2"},{k3:"v3"},{k4:"v4"}.........] into 5 select fields in a manner that ensures uniqueness at all times? For instance, if a value is selected in field1, it should not be available in the other ...

What is the method to extract and transform a numpy.ndarray into a different array?

Encountering an issue while attempting to load a numpy.ndarray and convert it into a new array. The goal is to preserve the data from the original numpy.ndarray. The process begins by using zip() to merge rows, columns, and weightings together. for connNa ...

Exploring the world of MVC4: Enhancing user experience with client-side

Solution: The answer provided by @www.innovacall.com is correct, I initially misunderstood it but now it works perfectly. Thank you for the help. Initial issue: I have been struggling with finding a solution to my problem. In my project, there is a mod ...

Incorporate a unique style designation with jquery's .css() method

Currently, I am facing an issue while working with Animate.css. I am attempting to set the duration, delay, and looping options but it seems like the command is not being applied. Upon investigation, I suspect that the propertyName is not being recognized ...