打破嵌套循环直到满足下一个条件

时间:2015-08-05 03:01:53

标签: c# asp.net-mvc for-loop foreach

我有以下代码我正在列出一个列表并将其划分为三列:

<div>
    @{
        double num = listing.Count() / 3.0;
        double blockSize = Math.Ceiling(num);
        for (int i = 0; i < Math.Ceiling(listing.Count() / blockSize); i++)
        {

            <div class="col-sm-4">
                @for (int j = i * (int)blockSize; j < (i * blockSize) + (int)blockSize && j < listing.Count(); j++)
                {
                    /// How can I only display this header once?
                    foreach(string letter in letters)
                    {
                        if (letter.ToLower() == listing.ElementAt(j).Title.Substring(0,1).ToLower())
                        {    
                            <h5 class="title"><a name="@letter.ToLower()">@letter.ToUpper()</a></h5>
                        }
                    }


                    <p><a href="#">@listing.ElementAt(j).Value</a></p>

                }                                    
            </div>
        }                       
    }
</div>

我的问题是如何将嵌套的foreach循环编码为每个字母匹配只有一次?产生如下输出:

<div class="row colIndentTop">
    <div class="col-sm-4">
        <h5 class="title"><a name="a">A</a></h5>
        <p><a href="#">Alice</a></p>
        <p><a href="#">Amy</a></p>


        <h5 class="title"><a name="d">D</a></h5>
        <p><a href="#">David</a></p>

        <h5 class="title"><a name="f">F</a></h5>
        <p><a href="#">Frank</a></p>

        <h5 class="title"><a name="i">I</a></h5>
        <p><a href="#">Ibrahim</a></p>
    </div>
    <div class="col-sm-4">
        <h5 class="title"><a name="n">N</a></h5>
        <p><a href="#">Nancy</a></p>

        <h5 class="title"><a name="o">O</a></h5>
        <p><a href="#">Olive</a></p>

        <h5 class="title"><a name="p">P</a></h5>
        <p><a href="#">Paul</a></p>                        
        <p><a href="#">Patrick</a></p>
    </div>
    <div class="col-sm-4">
        <p><a href="#">Patricia</a></p>

        <h5 class="title"><a name="s">S</a></h5>
        <p><a href="#">Sally</a></p>
        <p><a href="#">Sean</a></p>

        <h5 class="title"><a name="t">T</a></h5>
        <p><a href="#">Thomas</a></p>
    </div>
</div>

而不是:

<div class="row colIndentTop">
    <div class="col-sm-4">
        <h5 class="title"><a name="a">A</a></h5>
        <p><a href="#">Alice</a></p>
        <h5 class="title"><a name="a">A</a></h5>
        <p><a href="#">Amy</a></p>


        <h5 class="title"><a name="d">D</a></h5>
        <p><a href="#">David</a></p>

        <h5 class="title"><a name="f">F</a></h5>
        <p><a href="#">Frank</a></p>

        <h5 class="title"><a name="i">I</a></h5>
        <p><a href="#">Ibrahim</a></p>
    </div>
    <div class="col-sm-4">
        <h5 class="title"><a name="n">N</a></h5>
        <p><a href="#">Nancy</a></p>

        <h5 class="title"><a name="o">O</a></h5>
        <p><a href="#">Olive</a></p>

        <h5 class="title"><a name="p">P</a></h5>
        <p><a href="#">Paul</a></p>   
        <h5 class="title"><a name="p">P</a></h5>                     
        <p><a href="#">Patrick</a></p>
    </div>
    <div class="col-sm-4">
        <h5 class="title"><a name="p">P</a></h5>
        <p><a href="#">Patricia</a></p>

        <h5 class="title"><a name="s">S</a></h5>
        <p><a href="#">Sally</a></p>
        <h5 class="title"><a name="s">S</a></h5>
        <p><a href="#">Sean</a></p>

        <h5 class="title"><a name="t">T</a></h5>
        <p><a href="#">Thomas</a></p>
    </div>
</div>

提前致谢!

2 个答案:

答案 0 :(得分:1)

首先按字母顺序对你的名字(indexNavigation)进行排序。然后这样做(可能无法编译,我现在没有IDE):

<div>
    @{
        double num = listing.Count() / 3.0;
        double blockSize = Math.Ceiling(num);
        char currentLetter = '.'
        for (int i = 0; i < Math.Ceiling(listing.Count() / blockSize); i++)
        {

            <div class="col-sm-4">
                @for (int j = i * (int)blockSize; j < (i * blockSize) + (int)blockSize && j < listing.Count(); j++)
                {
                    var name = indexNavigation.ElementAt(j).Title;
                    if (currentLetter != name.Title.Substring(0,1).ToLower()[0])
                    {    
                        <h5 class="title"><a name="@letter.ToLower()">@letter.ToUpper()</a></h5>
                        currentLetter = name.Title.Substring(0,1).ToLower()[0];
                    }

                    <p><a href="Faculty_and_Research/Academic_Careers.html">@indexNavigation.ElementAt(j).Title</a></p>    
                }                                    
            </div>
        }                       
    }
</div>

答案 1 :(得分:1)

好。对于您拥有的整个代码,可能有更优雅的解决方案,但对于您的具体问题,您可以添加一个简单的检查。

此代码会将字母值添加到变量thisLetter并检查下一个foreach以确保您的字母是新的(否则letter != thisLetter将失败并且它将跳过添加h5)

var thisLetter = '';

foreach(string letter in letters)
{
    if ((letter.ToLower() == listing.ElementAt(j).Title.Substring(0,1).ToLower()) && letter != thisLetter)
    {    
        <h5 class="title"><a name="@letter.ToLower()">@letter.ToUpper()</a></h5>
    }
    thisLetter = letter;
}

您可以使用继续逻辑将其清理得像以下一样,这不会创建h5,也会跳过不必要的重新赋值给thisLetter。作为解释,continue将跳转到for循环的下一次迭代,而不执行循环中continue之后的任何代码。

var thisLetter = '';

foreach(string letter in letters)
{
    //Note!: The if logic is 'opposite' in this structure.
    if ((letter.ToLower() != listing.ElementAt(j).Title.Substring(0,1).ToLower()) || letter == thisLetter)
        continue;

    <h5 class="title"><a name="@letter.ToLower()">@letter.ToUpper()</a></h5>
    thisLetter = letter;
}