2013-07-05 57 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

它现在应该工作。是的,我的意图是使用当前的上下文。 –