简体   繁体   中英

create an array of anonymous type

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

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.)

Instead of using LINQ use foreach loops. You are more limited with LINQ. Also define your ArrayLists at the beginning and add to them as you go.

var test1 = new ArrayList();
var test2 = new ArrayList();
foreach(PageLoad p in _rep.GetMetricsData().GetLHDb().page_loads)
{
    if(p.t_3id == sID)
    {
        var tr = new { time = p.time, created_at = p.created_at };
        switch(p.test_path_id) 
        {
            case 1: test1.Add(tr); break;
            case 2: test2.Add(tr); break;
        }
    }
}
return Json(new { test1, test2, });

You do not need to define the names of properties anonymous types because they default to the variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM