将二维数组转换为对象c# [英] Converting two-dimensional array to an object c#
问题描述
GaData.Rows的格式如下所示:
每个维度都会有一行,在这个例子中,有一行是新访客,第二行是返回访客。在这些行中将会是另一组包含Dimension值的行,然后是您指定的每个度量标准(我只要求一个度量标准)。
到目前为止我设置的课程设置如下:
public class Results
{
public List< Dimension>维度{get;组; }
}
public class Dimension
{
public string Value {get;组; }
public List< Metric>度量标准{get;组; }
}
public class Metric
{
public int Value {get;组; }
}
最后,也许它迟到了,我的大脑运作不好,但我有点难以将这些数据转换为 Results
类型,我想是因为有多个图层。任何帮助?
编辑
我最终为GaData创建了一个扩展方法,它叫做ToDimensionResults()。我不确定是否能够使用LINQ来完成此操作,因为我需要知道某些行的索引(如维度值)。所以我选择了循环维度和指标并手动创建类。注意:如果您的查询中未包含维度,则结果中不包含维度值,只包含一系列指标,因此可以适应这种可能性。
public static Results ToDimensionResults(this GaData ga)
{
var results = new Results();
var dimensions = new List< Dimension>();
List< Metric>指标;
var value =;
var metricStartIndex = 1;
for(var i = 0; i< ga.Rows.Count; i ++)
{
//容纳没有尺寸的数据
if(!string。 IsNullOrEmpty(ga.Query.Dimensions))
{
value = ga.Rows [i] [0] .ToString();
}
else
{
value =;
metricStartIndex = 0;
}
metrics = new List< Metric>(); (var x = metricStartIndex; x {
metrics.Add(new Metric
{
Value = Convert.ToInt32(ga.Rows [i] [x])
});
}
dimensions.Add(新维度
{
值=值,
度量=指标
});
}
results.Dimensions = dimensions;
返回结果;
}
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!
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;
}
这篇关于将二维数组转换为对象c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!