0

我认为我需要使用一个 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; }
  }

每行包含值:

  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 的长度?智能感知没有给我那个属性

风景:

 <% 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>

谢谢

4

2 回答 2

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;
}

正如Jared所提到的,如果您的收藏量很大,您可能希望使用StringBuilderas 在性能上要好得多。

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();
}
于 2013-01-09T07:29:49.877 回答
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()扩展方法。

于 2013-01-09T07:44:59.557 回答