lambda表达式:根据另一个属性获取属性

时间:2015-02-05 23:49:32

标签: c# asp.net-mvc lambda

我正在尝试在我的视图中构建HTML:

<div class="row contentHeaderInfo">
     @foreach (var groupedItems in Model.DSet.GroupBy(a => new {a.rowId}))
     {             
        <ul>
            @foreach (var item in groupedItems)
            {
                 <li>@item.Label  @item.labelValue</li>
            }
        </ul>            
     }
</div>

rowid = 1的对象内的数据:

groupedItems[0] = {1,"FName","ABC"}
groupedItems[0] = {1,"LaName","DEF"}
groupedItems[0] = {1,"Address",""}
groupedItems[0] = {1,"City","LosAngeles"}
groupedItems[0] = {1,"State","California"}
groupedItems[0] = {1,"Zip","90045"}
groupedItems[0] = {1,"Street","Cardiff"}
groupedItems[0] = {1,"House No","1234"}
groupedItems[0] = {1,"Apt","1"}

对于某些label我想在labelValues代码中添加多个<a>

例如,如果@item.Label = "Address",那么我想要

 <li> Address <a href ="blah"> 1234 Cardiff LosAngeles California 90045 </a></li>

其余的(apt,house no,city,state,zip)不会单独出现。是否可以使用lambda表达式来实现这一目标?

1 个答案:

答案 0 :(得分:1)

为什么不使用@if?

@foreach (var item in groupedItems)
{
    @if (item.Label == "zip")
    {
        <li>@item.Label  @item.labelValue</li>
    }
}