Converting two-dimensional array to an object c#

本小妞迷上赌 提交于 2020-01-03 05:35:06

问题


This all originates from querying Google Analytics data. For a basic query the main factors that change are the dimensions and the metrics. The object that is returned is of a type called GaData, and the actual results that you need reside in GaData.Rows.

The format of GaData.Rows looks like this:

There will first be a row for each dimension, in this example there is a row for "New Visitor" and a 2nd row for "Returning Visitor". Within those rows will be another set of rows that contain the Dimension value, and then each metric that you specify (I've only asked for one metric).

So far the class setup I have is as follows:

public class Results
{
    public List<Dimension> Dimensions { get; set; }
}

public class Dimension
{
    public string Value { get; set; }
    public List<Metric> Metrics { get; set; }
}

public class Metric
{
    public int Value { get; set; }
}

Finally, maybe its just late and my brain isn't functioning well, but I'm having a little bit of difficulty converting this data into the Results type, I think because of the multiple layers. Any help?

Edit

I added an answer below for how I ended up accomplishing it, if anyone has a more condensed example let me know!


回答1:


Well, I don't know what Rows is inside Ga, but maybe this will point you in the right direction.

var results
    = GaData.Rows.Select(x => x.Rows.Select(y =>
          new Dimension { Value = y.Value, Metrics = new List<Metric> {innerRow.Metric}}));



回答2:


I ended up creating an extension method for GaData called ToDimensionResults(). I'm not sure if I would have been able to accomplish this using LINQ as I needed to know the index of some of the rows (like the Dimension Value). So I opted to just loop through both dimensions and metrics and create the class manually. NOTE: if you do not include a dimension in your query, the results do not contain the dimension value, only a list of metrics, so this accommodates that possibility.

    public static Results ToDimensionResults(this GaData ga)
    {
        var results = new Results();
        var dimensions = new List<Dimension>();
        List<Metric> metrics;
        var value = "";
        var metricStartIndex = 1;

        for (var i = 0; i < ga.Rows.Count; i++)
        {
            //accomodate data without dimensions
            if (!string.IsNullOrEmpty(ga.Query.Dimensions))
            {
                value = ga.Rows[i][0].ToString();
            }
            else
            {
                value = "";
                metricStartIndex = 0;
            }

            metrics = new List<Metric>();

            for (var x = metricStartIndex; x < ga.Rows[i].Count; x++)
            {
                metrics.Add(new Metric
                {
                    Value = Convert.ToInt32(ga.Rows[i][x])
                });
            }


            dimensions.Add(new Dimension
            {
                Value = value,
                Metrics = metrics
            });
        }

        results.Dimensions = dimensions;

        return results;
    }


来源:https://stackoverflow.com/questions/20532660/converting-two-dimensional-array-to-an-object-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!