如何将字符串数组转换为IList <sometype>?

时间:2015-05-27 16:35:48

标签: c# linq

我试图将Datatable列转换为Series数组(我稍后将其用于将它们序列化为Json),但不确定如何将字符串数组转换为IList?或许我应该使用其他方法?但不确定如何将字符串数组转换为IList<SeriesValue>?或者也许我应该使用其他方法?

public Series[] GetGraphData(string sp)
    {
        var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
        using (var da = new SqlDataAdapter("exec " + sp, connection))
        {
            var dt = new DataTable();

            da.Fill(dt);
            da.FillSchema(dt, SchemaType.Mapped);

            Series[] arrSeries = new Series[dt.Columns.Count];

            foreach(DataColumn dc in dt.Columns)
            {
                if (dc.Ordinal == 0)
                {

                }
                else
                {
                    Series s = new Series()
                    {
                        seriesname = dc.ColumnName,
                        renderas = "Line",
                        data = dt.Rows.Cast<DataRow>().Select(row => row[dc.Ordinal]).Cast<SeriesValue>().ToList()
                    };

                    arrSeries[dc.Ordinal] = s;
                }
            }

            return arrSeries;
        }
    }



 public class Series
{
    public string seriesname { get; set; }
    public string renderas { get; set; }
    public IList<SeriesValue> data { get; set; }
}

public class SeriesValue
{
    public string value { get; set; }
}

Json格式示例:

{
                        "seriesname": "Input",
                        "renderas": "Line",
                        "data": [
                            {
                                "value": "6500"
                            },
                            {
                                "value": "6500"
                            },
                            {
                                "value": "6500"
                            }
}

非常感谢任何帮助?

1 个答案:

答案 0 :(得分:5)

您可以将字符串转换为如下对象:

给出:strarr =字符串数组

data = strarr.Select(o => new SeriesValue { value = o }).ToList()

现在数据是List<SeriesValue>,它实现了IList<SeriesValue>