2013-01-09 50 views
0

我需要使用for循環,我想我可以訪問數組中每一行中的每個屬性來組成我的字符串。字符串使用「foreach」或「for」構造數組使用「foreach」或「for」循環構造

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString) 
    { 

    foreach (var part in HintTextString) 
    { 
     HintText = HintText + " " + part.Name + " " + part.IncentiveTrainingModuleOptionName + " = " + part.Points; 
    } 

    return HintText; 
    } 

    public class UserTrainingPointsDataModel 
    { 
    public virtual int InteractionType { get; set; } 
    public virtual int Points { get; set; } 
    public virtual string Name { get; set; } 
    public virtual string IncentiveTrainingModuleOptionName { get; set; } 
    } 

每一行constains值:

China Incentive Program Points For Completing = 50 
    India - Q2 Incentive Points Per Correct Answer = 50 
    China - Q2 Incentive Points For Completing = 50 
    India Incentive Program Points Per Correct Answer = 100 
    India - Q2 Incentive Points Per Correct Answer = 100 

HintTextString包含每個5行以上的性能,我需要補的字符串,看起來像這樣:

"Incentive Program: " + part.Name[0] + " " + part.IncentiveTrainingModuleOptionName[0] + " + " + part.Points[0] + 
    "Incentive Program: " + part.Name[1] + " " + part.IncentiveTrainingModuleOptionName[1] + " + " + part.Points[1] 

等。所以我需要一個for循環呢?以及如何獲得HintTextString的長度? IntelliSense不給我這個屬性

的觀點:

<% if (module.HasAssessment) 
{ %> 
    <div class="<%: moduleStateClass %>">&nbsp;</div> 
    <div class="<%: moduleScoreClass %>"><%: module.ModuleScore %></div> 
    <% var HintText = TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %> 

    <script type="text/javascript"> 
     <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: " + HintText , "training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %> 
    </script> 
<% } %> 
<% else 
    { %> 
<% var HintText = TrainingModuleProgressStateDataModelExtentions.GetHintText(module.UserTrainingPoints); %> 
<script type="text/javascript"> 
    <%: Html.GetQTip("training-module-id-" + module.TrainingModuleId , "Incentive program: " + HintText ,"training-module-id-" + module.TrainingModuleId , Zinc.Web.Extensions.QTipPosition.Bottom, true, "Module Points") %> 
</script> 
<% } %>  

「行動」:

<div class="module-column-content"> 
    <% Html.RenderPartial("~/Areas/Training/Views/Home/Details.ascx", Model); %> 
    </div> 

感謝

+0

所以你要爲一個刺痛返回整個列表 –

+0

後產生的結果 – Jared

+0

也發佈視圖如果可能的話 – Jared

回答

1

如果你只是想返回整個列表作爲一個刺在你描述的格式中應該可以工作

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString) 
{ 
    string returnValue = string.Empty; 
    foreach (var dataModel in HintTextString) 
    { 
     returnValue += string.Format("Incentive Program: {0} {1} + {2} ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points); 
    } 
    return returnValue; 
} 

作爲賈裏德提到,如果你的收藏很大,你可能想要使用StringBuilder作爲更好的性能。

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString) 
{ 
    var returnValue = new StringBuilder(); 
    foreach (var dataModel in HintTextString) 
    { 
     returnValue.AppendFormat("Incentive Program: {0} {1} + {2} ", dataModel.Name, dataModel.IncentiveTrainingModuleOptionName, dataModel.Points); 
    } 
    return returnValue.ToString(); 
} 
+0

噢噢是的,我忘了HANDELING字符串這樣的動作,謝謝 –

+0

在做快速字符串處理時應該使用'StringBuilder' ...如果數組/列表中有5000個項目,這可能是非常「昂貴的」 – Jared

+0

@Jared,這是一個很好的觀點,我現在會更新:) –

0

使用StringBuilder構建文本,同時迭代集合。它經過優化,可以使用字符串操作功能。如果在循環中使用簡單的文本添加(+=),則性能會變得糟糕透頂。

public static string GetHintText(this IEnumerable<UserTrainingPointsDataModel> HintTextString) 
{ 
    //format for each item 
    //{Name} {ModuleName} = {Points} <NewLine> 
    string lineFormat = "Incentive Program: {0} {1} = {2}\r\n"; 
    StringBuilder sb = new StringBuilder(); 
    foreach (var part in HintTextString) 
    { 
    sb.AppendFormat(lineFormat, part.Name, part.IncentiveTrainingModuleOptionName, part.Points); 
    } 

    return sb.ToString(); 
} 

另外,如果你需要的HintTextString集的長度,你可以使用IEnumerable.Count()擴展方法。