2013-07-05 59 views
1

我得到下列類別的列表。用逗號連接字符串以加入長字符串

public class SmsResponse 
    { 
     public string AliasName { get; set; } 
     public string CellPhoneNumber { get; set; } 
     public int Response { get; set; } 
    } 

我通過這個列表的功能,以檢查是否響應字段比0的響應等,如果有比它爲我所用這種方法PrepareStatusString();準備狀態字符串錯誤。

bool isSuccess = EvaluateSmsResponse(responseList); //list of smsresponse class 

private bool EvaluateSmsResponse(List<SmsResponse> smsResponseList) 
    { 
     bool isSent = smsResponseList.Exists(response => response.Response != 0); 
     if (!isSent) 
      PrepareStatusString(smsResponseList); 
     return isSent; 
    } 

    private void PrepareStatusString(List<SmsResponse> responseList) 
    { 
     bool isfirst = true; 
     foreach (var item in responseList) 
     { 
      if (item.Response != 0) 
      { 
       if(isfirst) 
        StatusDescription += item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString(); 
       else 
        StatusDescription += "," + item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString(); 

       isfirst = false; 
      } 
     } 
    } 

該代碼工作正常,但可以通過任何方式進行優化/改進。我感覺有一個範圍改善,但無法弄清楚?

回答

4

如果您使用.NET 4或更新版本,則可以覆蓋SmsResponse.ToString(),然後使用String.Join<T>(String, IEnumerable<T>)來連接響應。

所以你SmsResponse類可能是這個樣子:

public class SmsResponse 
{ 
    public string AliasName { get; set; } 
    public string CellPhoneNumber { get; set; } 
    public int Response { get; set; } 

    public override string ToString() 
    { 
     return AliasName + "|" + CellPhoneNumber + "|" + 
      Response.ToString(); 
    } 
} 

而且PrepareStatusString是:

private void PrepareStatusString(List<SmsResponse> responseList) 
{ 
    StatusDescription = string.Join(",", responseList.Where(i => i.Response != 0)); 
} 
+0

我正在使用.NET框架4.0,也喜歡的解決方案,但它給了我的函數的字符串關鍵字公開覆蓋字符串ToString()聲明「期望的類,委託,枚舉,接口或結構」錯誤 – ankur

+0

@ankur看起來你已經將'ToString()'方法複製到命名空間範圍內,而不是類範圍。 –

+0

是超出了課程範圍。它工作完全乾淨和簡單.... – ankur

4

StringBuilder將追加foreach循環中的字符串更高效(取決於迭代NUM)

private void PrepareStatusString(List<SmsResponse> responseList) 
{ 
    bool isfirst = true; 
    StringBulder sb = new StringBuilder(); 
    foreach (var item in responseList) 
    { 
     if (item.Response != 0) 
     { 
      if(isfirst) 
       sb.AppendFormat("{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber,item.Response.ToString()); 
      else 
       sb.AppendFormat(",{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber, item.Response.ToString()); 

      isfirst = false; 
     } 
    } 

    StatusDescription = sb.ToString(); 
} 
0

使用string.Join像這樣

List<string> elements = new List<string>(); 
    foreach (var item in responseList) 
    { 
     if (item.Response != 0) 
     { 
      elements.add(item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString()); 
     } 
    } 
    string result = string.Join(",", elements.ToArray()); 
1

我不知道優化,但它可以被更明確地重寫如下:

private void PrepareStatusString(List<SmsResponse> responseList) 
{ 
    StatusDescription = responseList 
     .Where(x => x.Response != 0) 
     .Select(x => x.AliasName 
        + "|" + x.CellPhoneNumber 
        + "|" + x.Response.ToString()) 
     .Aggregate((x, y) => x + "," + y); 
} 

請注意,StringBuilder只會提供顯着的性能優勢,如果您期望有超過數百個對象的話。

+0

它給項錯誤,因爲它不會在目前的情況下存在。你打算在循環中使用這個LINQ語句... – ankur

+0

它現在應該工作。是的,我的意圖是使用當前的上下文。 –