C# 如何将字符串数组转换为IList<;SomeType>;?
我试图将Datatable列转换为Series数组(稍后我将使用它将它们序列化为Json),但不确定如何将string数组转换为IList?或者我应该用另一种方法?但不确定如何将字符串数组转换为IList?或者我应该用另一种方法C# 如何将字符串数组转换为IList<;SomeType>;?,c#,linq,C#,Linq,我试图将Datatable列转换为Series数组(稍后我将使用它将它们序列化为Json),但不确定如何将string数组转换为IList?或者我应该用另一种方法?但不确定如何将字符串数组转换为IList?或者我应该用另一种方法 public Series[] GetGraphData(string sp) { var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].Connection
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; }
}
非常感谢您的帮助 您可以将字符串转换为如下对象: 给定:strarr=字符串数组
data = strarr.Select(o => new SeriesValue { value = o }).ToList()
现在数据是一个
列表
,它实现了IList
只是arrSeries.ToList()
。对不起,这不是我想要的。这行代码有一个问题:data=dt.Rows.Cast().Select(row=>row[dc.Ordinal]).Cast().ToList()当SeriesValue{}只包含一个字符串时,它的意义是什么?因为我需要将它们序列化为Json中的特定格式。除此之外,字符串数组已经是IList了<代码>IList列表=新[]{“a”,“b”}代码>完美,正是我想要的
data = strarr.Select(o => new SeriesValue { value = o }).ToList()