我有一个双打列表List<double[]>
,我想将其转换为List<T>
其中 T 是一个类。
double 数组包含 17 个值,例如 [1.0, 2.0, 3.0, 4.0, 5.0,.. 17.0 ]。然后我有一个类,它定义了 17 个字符串属性,例如 P1、P2、....、P17
所以 an 的每个元素List<double[]>
都是一个双精度数组,并且数组中的每个元素都代表 type 类中属性的一个值T
。
是否可以将给定双精度数组的每个索引映射到 T 类型的属性。所以我将 a 转换List<double[]>
为List<T>
where T
is class
。
我知道这可以通过手动迭代列表来完成,读取每个数组,然后从数组的每个索引中读取值并将其传递给类的相应属性。但是当我有很多具有 10 多个属性的类时,它有很多事情要做。
编辑:下面给出了其中一个类的示例
/// <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