c# – 将列表转换为列表
发布时间:2020-12-16 01:54:48 所属栏目:百科 来源:网络整理
导读:我有一个双打列表列表 double []我想将其转换为List T其中T是一个类. double数组包含17个值,例如[1.0,2.0,3.0,4.0,5.0,.. 17.0]. 然后我有一个类定义了17个字符串属性,例如P1,P2,….,P17 所以列表的每个元素 double []是一个双精度数组,数组中的每个元素表示
我有一个双打列表列表< double []>我想将其转换为List< T>其中T是一个类.
double数组包含17个值,例如[1.0,2.0,3.0,4.0,5.0,.. 17.0]. 所以列表的每个元素< double []>是一个双精度数组,数组中的每个元素表示类型为T的类中的属性值. 是否可以将给定的双精度数组的每个索引映射到类型为T的类的属性.所以我转换一个List< double []>列出< T> T是班级. 我知道可以通过手动迭代读取每个数组的列表来完成 编辑:下面给出了其中一个类的示例 /// <summary> /// Defines the properties of a centroid. /// </summary> public class Centroid { // ReSharper disable InconsistentNaming /// <summary> /// Calls made to contacts /// </summary> public string CONTACT_CALLS { get; set; } /// <summary> /// Duration of calls made to contacts /// </summary> public string CONTACT_CALLS_SEC { get; set; } /// <summary> /// Calls made to non contacts /// </summary> public string UNKNOWN_CALLS { get; set; } /// <summary> /// Duration of calls made to non contacts /// </summary> public string UNKNOWN_CALLS_SEC { get; set; } /// <summary> /// Number of SMS sent to contacts /// </summary> public string SMS_OUT_CONTACTS { get; set; } /// <summary> /// Number of SMS sent to non contacts /// </summary> public string SMS_OUT_UNKNOWN { get; set; } /// <summary> /// Percentage of CPU usaed /// </summary> public string CPU_USAGE { get; set; } /// <summary> /// Percentage of RAM used /// </summary> public string RAM_USAGE { get; set; } /// <summary> /// Number of system application /// </summary> public string SYS_APPS { get; set; } /// <summary> /// Number of user applications /// </summary> public string USER_APPS { get; set; } /// <summary> /// Number of system services /// </summary> public string SYS_SERVICES { get; set; } /// <summary> /// Number of user services /// </summary> public string USER_SERVICES { get; set; } /// <summary> /// Number of bytes sent /// </summary> public string BYTES_TX { get; set; } /// <summary> /// Number of bytes received /// </summary> public string BYTES_RX { get; set; } /// <summary> /// Latitute of the location /// </summary> public string LOC_LAT { get; set; } /// <summary> /// Longitude of the location /// </summary> public string LOC_LON { get; set; } /// <summary> /// The Time Slice /// </summary> public string TIME_SLICE { get; set; } // ReSharper restore InconsistentNaming } EDIT2:从列表创建列表的方法,注意_profiler.FinalCentroid的类型为List< double []> private void CreateListOfCentroids() { _centroidsList = new CentroidsList {Centroids = new List<Centroid>()}; foreach (var centroid in _profiler.FinalCentroid.Select(doublese => new Centroid { CONTACT_CALLS = doublese[0].ToString(CultureInfo.InvariantCulture),CONTACT_CALLS_SEC = doublese[1].ToString(CultureInfo.InvariantCulture),UNKNOWN_CALLS = doublese[2].ToString(CultureInfo.InvariantCulture),UNKNOWN_CALLS_SEC = doublese[3].ToString(CultureInfo.InvariantCulture),SMS_OUT_CONTACTS = doublese[4].ToString(CultureInfo.InvariantCulture),SMS_OUT_UNKNOWN = doublese[5].ToString(CultureInfo.InvariantCulture),CPU_USAGE = doublese[6].ToString(CultureInfo.InvariantCulture),RAM_USAGE = doublese[7].ToString(CultureInfo.InvariantCulture),SYS_APPS = doublese[8].ToString(CultureInfo.InvariantCulture),USER_APPS = doublese[9].ToString(CultureInfo.InvariantCulture),SYS_SERVICES = doublese[10].ToString(CultureInfo.InvariantCulture),USER_SERVICES = doublese[11].ToString(CultureInfo.InvariantCulture),BYTES_TX = doublese[12].ToString(CultureInfo.InvariantCulture),BYTES_RX = doublese[13].ToString(CultureInfo.InvariantCulture),LOC_LAT = doublese[14].ToString(CultureInfo.InvariantCulture),LOC_LON = doublese[15].ToString(CultureInfo.InvariantCulture),TIME_SLICE = doublese[16].ToString(CultureInfo.InvariantCulture) })) { _centroidsList.Centroids.Add(centroid); } }//end method 解决方法
要发布明显的,为什么不在构造函数中分配属性?
您将开始创建属性的工作. 在构造函数中分配值的键击次数少于属性. List<double[]> ld = new List<double[]>(); List<PropDouble> lpd = new List<PropDouble>(); foreach (double[] da in ld) { lpd.Add(new PropDouble(da)); } public class PropDouble { public double P0 { get; set; } public double P1 { get; set; } public PropDouble(double[] doubles) { P0 = doubles[0]; P1 = doubles[1]; } } 要么 public class PropDouble { private double[] doubles; public double P0 { get { return doubles[0]; } set { doubles[0] = value; } } public double P1 { get { return doubles[1]; } set { doubles[1] = value; } } public PropDouble(double[] Doubles) { doubles = Doubles; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |