我有两个类 - Record
和RecordModified
转换为另一种类型的
public class Record
{
public int RecordID { get; set; }
public int FacilityID { get; set; }
public int NewAID { get; set; }
public string OldID { get; set; }
public string Data { get; set; }
public int SyncStatusID { get; set; }
public int RecordTypeID { get; set; }//RecordTypeID is integer here.
}
第二类
public class RecordModified
{
public int RecordID { get; set; }
public int FacilityID { get; set; }
public int NewAID { get; set; }
public string OldID { get; set; }
public string Data { get; set; }
public int SyncStatusID { get; set; }
public string RecordTypeText { get; set; }//RecordTypeText is string here.
}
我有记录与至少100 Record
对象的列表,现在我必须将List<Record>
转换为List<RecordModified>
。 Record
类的RecordTypeID
财产必须转换为RecordModified
的财产RecordTypeText
使用enum
s在不同的类。关于如何我想转换
代码片段:
foreach(Record r in List<Record>)
{
switch(r.RecordTypeID)
{
case (int)MyEnum.One:
listofRecordModified.Add(new RecordModified{RecordTypeID=r.RecordTypeID,...,**RecordTypeText=(MyEnum.One).ToString()})** // Notice this
break;
...........//75 more cases.
}
这个解决方案工作得很好,但问题是很多的代码,我不认为它的效率。必须有一些更好的方法来做到这一点。请建议。
@Patrick福满沿...感谢您的编辑 – dividedbyzero 2015-03-02 07:58:48