简体   繁体   English

创建一个匿名类型的数组

[英]create an array of anonymous type

I am trying to get data from database for Google charts in my program. 我正在尝试从我的程序中的Google图表数据库中获取数据。 I would like to create an array of anonymous type (var) instead of repeating my code over and over again: 我想创建一个匿名类型(var)数组,而不是一遍又一遍地重复我的代码:

 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 }; 我知道我将需要一个for循环所以我可以通过所有的测试路径id而不是硬编码它们像p.test_path_id == 1 ,但我的问题是我将如何使这部分动态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. 如果你有一个数组而不是一个列表真的非常重要,那么你可以在最后调用myList上的ToArray 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. 另一方面,List用于随着时间的推移改变它的大小,因此它可以从0或1项开始,然后随着时间的推移添加项,这对于我们在这个特定的上下文中很重要。 (It's actually useful quite often, which is why it is frequently useful to use List over arrays.) (它实际上非常有用,这就是为什么在数组上使用List通常很有用。)

Instead of using LINQ use foreach loops. 而不是使用LINQ使用foreach循环。 You are more limited with LINQ. LINQ更受限制。 Also define your ArrayLists at the beginning and add to them as you go. 还可以在开头定义ArrayLists并随时添加它们。

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. 您不需要定义属性匿名类型的名称,因为它们默认为变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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