Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:50:50+00:00 2026-06-05T10:50:50+00:00

I am trying to get data from database for Google charts in my program.

  • 0

I am trying to get data from database for Google charts in my program. I would like to create an array of anonymous type (var) instead of repeating my code over and over again:

 public JsonResult GetChartData(int sID, int regionID)
    {

            var testPathOne = from p in _rep.GetMetricsData().GetLHDb().page_loads                                 
                              where p.t_3id == sID && p.test_path_id == 1
                              select new { time = p.time, created_at = p.created_at };

            var testPathTwo = from p in _rep.GetMetricsData().GetLHDb().page_loads
                              where p.t_3id == sID && p.test_path_id == 2
                              select new { time = p.time, created_at = p.created_at };

            var tOne = testPathOne.ToArray();
            var tTwo = testPathTwo.ToArray();
            var name = new { test1 = tOne, test2 = tTwo };
            return Json(name);
        }

i know that i will need a for loop so i can go through all the test path id’s instead of hard coding them like this p.test_path_id == 1, but my question is how would i make this part dynamic var name = new { test1 = tOne, test2 = tTwo };

Edit:
I apologize, I would like to do something like this:

name is an array

for loop:
    testPath = query
    name.Add(testPath)

I hope that makes sense

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:50:51+00:00Added an answer on June 5, 2026 at 10:50 am

    The easiest solution in this particular case would be to just give a name to the class that is currently anonymous. While there are workarounds that you can use, when you need to start working really hard to use an anonymous type you probably shouldn’t be using it. It’s there to make certain tasks quicker and easier; if that isn’t happening then you are likely better off with a real class.

    That solution would look something like this:

    //Please give me a real name
    public class ClassToBeRenamed
    {
        public DateTime Time { get; set; }
        public DateTime CreatedAt { get; set; }
    }
    
    List<ClassToBeRenamed[]> myList = new List<ClassToBeRenamed[]>();
    
    for (int i = 0; i < 10; i++)
    {
    myList.Add((from p in _rep.GetMetricsData().GetLHDb().page_loads
                where p.t_3id == sID && p.test_path_id == i
                select new ClassToBeRenamed { Time = p.time, CreatedAt = p.created_at })
                .ToArray());
    }
    

    Having said all of that, it’s still possible.

    var myList = new[]{
                    from p in _rep.GetMetricsData().GetLHDb().page_loads
                    where p.t_3id == sID && p.test_path_id == 1
                    select new { time = p.time, created_at = p.created_at }.ToArray()
    }.ToList();
    
    for (int i = 2; i < 10; i++)
    {
        myList.Add(from p in _rep.GetMetricsData().GetLHDb().page_loads
                    where p.t_3id == sID && p.test_path_id == i
                    select new { time = p.time, created_at = p.created_at }.ToArray()
                    );
    }
    
    var myArray = myList.ToArray();
    

    If it’s really, really important that you have an array, and not a list, then you could call ToArray on myList at the very end. It’s important that you start out with a list, and only convert it to an array at the end because Arrays have a fixed size once they are created. You can mutate their contents, but you can’t make them bigger or smaller. A List on the other hand, is designed to mutate it’s size over time, so it can start out with 0 or 1 items and then add items over time, which is important for us in this particular context. (It’s actually useful quite often, which is why it is frequently useful to use List over arrays.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example I'm trying to get the data from database like: using (ExplorerDataContext context
I'm trying to get data from the database in order to create a list
I am trying to fill gridpanel to get data from database through LINQ in
Actually i was trying to get data from elgg database based on multiple joins.
I'm trying to put some geolocation data from Google Maps into a database. But
I'm trying to get data from database, and echo them on the page using
I'm trying to get data from a database using FMDB but having tried with
I'm trying to get data from database according to the item selected in the
I'm trying to get data from another database using the OPENDATASOURCE command, but I
Ok, i'm trying to get data from a database to be pulled according to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.