迭代Dictionary的最佳方法<string,dictionary <string,=“”object =“”>&gt;在NVelocity

时间:2016-11-30 11:43:24

标签: c# nvelocity

我有一个C#Dictionary&lt; string,Dictionary&lt; string,object&gt;&gt;,我试图用nVelocity模板显示数据,我也试图用索引获取字典的键。我是新手。

请帮我讲述如何使用索引访问密钥,例如我想访问0索引处的键,以及如何使用迭代显示数据以及如何在foreach循环中放入“break”语句?

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tbody>
        <tr class="HeaderOne">
            <td align="left">Class</td>

            #foreach($services in $VolumeSummary)
                #foreach($item in $VolumeSummary[$services])
                    <td class="numeric-th">$item.AverageUnitPriceF</td>
                #end
                #break
            #end
            <td class="numeric-th">Average Price</td>
            <td class="numeric-th">Total</td>
        </tr>

        #foreach ($item in $TotalsByServiceAndTermYear)
        <tr style="color:#333;font-weight:normal;">
            <td>$item.Service</td>
            <td class="numeric-td">
                $!item.Year
            </td>
            <td class="numeric-td">
                $!item.AverageUnitPriceF
            </td>
            <td class="numeric-td">
                $!item.TotalPriceF
            </td>
        </tr>
        #end
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

NVelocity只是坐在.NET对象上,因此您无法通过索引从字典中获取项目,但您可以使用$dict.get_Item("key")按键获取项目,这是索引器的语法这是项索引器的基础CLR方法。

正如其他人所提到的,Dictionary具有不确定的排序顺序,请考虑使用SortedDictionary

您可以使用#foreach指令从#break中断,就像您的示例一样。它是在我们发布v1.1.1之后于2011年实现的,因此您必须从master构建或从我们的构建服务器获取主构建,否则您可以使用条件来隐藏内容来解决它。

#foreach($item in $dict)
  $item.Key
  #break
#end

#foreach($item in $dict)
  #if($velocityCount == 1)
    $item.Key
  #end
#end