Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
How can a linq query be converted to JavaScript array containing only values?

I have a JavaScript grid library (it creates a table on the page) that accepts a JavaScript array as input, for rendering in the grid. I'm not certain, however, how to convert a Linq-to-SQL query (against a SQL Server database) to a JavaScript array containing only values.

I tried this, but it included the table column names in the JSON key (and I don't want JSON anyway, I want a JavaScript string array, unless this can be converted to an array?):

JsonConvert.SerializeObject(query)

Example of the format I need to produce:

[1,2,3],[4,5,6]

Environment: .NET Core 3.1

edit: Here is a sample of what I've currently got, this returns the less than desirable JSON (due to the query results being so large, having a JSON key for very element is going to literally double the size of the query):

Devices Table

ID   Name
1    iPhone7
2    iPhone8
3    iPhone9

Needed Array (Note: no column names)

[1, "iPhone7"],[2, "iPhone8"],[3, "iPhone9"]

Current C# code in the controller method (returns undesirable key for every element currently)

var query = db.Devices;
var formattedResult = JsonConvert.SerializeObject(query);
return Ok(formattedResult);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Technically, you could do this:

var query = db.Devices.AsEnumerable()
    .Select(d => new object[]{d.ID, d.Name});
var formattedResult = JsonConvert.SerializeObject(query);
return Ok(formattedResult);

But then the code on the other end of your request is going to have to translate all those arrays back into objects.

It's rarely worthwhile to complicate your model like this in order to optimize the size of your network traffic. If you're pulling enough items over the wire to make this a performance issue, you're likely to encounter a variety of other performance issues. I would first consider other options, like implementing paging.

about 4 years ago · Juan Pablo Isaza Report

0

Did you try

var query = db.Devices.ToList(); 
var array = JArray.FromObject(query);
return Ok(formattedResult)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!