我有兩個類 - 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