打破不可分为两部分

时间:2012-02-25 18:24:02

标签: asp.net-mvc-3 c#-4.0

我有一个IEnumerable类型的集合。我想这样表达:

a)按日期从集合中获取第一个元素,并在标题1下显示 b)撤销剩余的收集并显示标题2下的所有剩余要素。

以下是我创建的

<ul style="list-style-type: none;">
    @{

        var responses = dispute.DisputeTransactions.GetTransactionByLogOnType(((CreditRegistryPrincipal)User).LogOnType);
        var first = responses.OrderBy(dd => dd.TransactionDate).FirstOrDefault();
        responses = responses.OrderByDescending(dd => dd.TransactionDate);
    }
    @if (dispute.Response > 0)
    {
        <h3 style="margin-left: -15px; margin-bottom: 10px;">
            Responses and Feedbacks</h3>
    }
    @foreach (DisputeTransaction dt in responses)
    {
        if (dt.DisputeTransactionId == first.DisputeTransactionId)
        {

            // firstConversation = false;
        <h3 style="margin-bottom: 10px">
            Dispute</h3>
        <ul class="firstConversation" style="margin-left: -15px">
            <div class="dispute-notes">
                @dt.Notes
            </div>

        </ul>

        }
        else
        {
            // responses = responses.SkipWhile(dd => dd.DisputeTransactionId == first.DisputeTransactionId);



        <li class="dispute-item @(user.RegistryID == dt.ReportedById ? "mine" : "their")">
            <div>
                <span class="dispute-writter"><b>@dt.ReportedBy.FullName</b> on @CreditRegistryRepository.FormatDateTime(dt.TransactionDate)
                    wrote:<br />
                </span>
                <div class="dispute-content">
                    <div class="dispute-notes">
                        @dt.Notes
                    </div>

                </div>
            </div>
        </li>
        }
    }
</ul>

但它在底部显示了heading1。如何使标题一和第一个元素在顶部。请建议

2 个答案:

答案 0 :(得分:1)

您也可以尝试跳过两个OrderBy

var responses = dispute
                    .DisputeTransactions
                    .GetTransactionByLogOnType(((CreditRegistryPrincipal)User).LogOnType)
                    .OrderBy(dd => dd.TransactionDate);
var first = responses.FirstOrDefault();
responses = responses.Skip(1).Reverse();

反转总是比OrderBy


@{
   var responses = new List<string>() { "1", "2", "3", "4", "5" };
   var first = responses.First();
   var allButOne = responses.Skip(1).Reverse();
}
<h1>@first</h1>
<ul>
   @foreach (var a in allButOne)
   {
      <li>@a</li>
   }
</ul>

enter image description here

答案 1 :(得分:0)

只是不要在循环中使用它,因为你正在迭代降序集合

<ul style="list-style-type: none;">
@{

    var responses = dispute.DisputeTransactions.GetTransactionByLogOnType(((CreditRegistryPrincipal)User).LogOnType);
    var first = responses.OrderBy(dd => dd.TransactionDate).FirstOrDefault();
    responses = responses.OrderByDescending(dd => dd.TransactionDate);
}
@if (dispute.Response > 0)
{
    <h3 style="margin-left: -15px; margin-bottom: 10px;">
        Responses and Feedbacks</h3>
}

<h3 style="margin-bottom: 10px">
   Dispute</h3>
<ul class="firstConversation" style="margin-left: -15px">
    <div class="dispute-notes">
        @dt.Notes
    </div>
</ul>
@foreach (DisputeTransaction dt in responses)
{
    if (dt.DisputeTransactionId != first.DisputeTransactionId)
    {
        // responses = responses.SkipWhile(dd => dd.DisputeTransactionId == first.DisputeTransactionId);



    <li class="dispute-item @(user.RegistryID == dt.ReportedById ? "mine" : "their")">
        <div>
            <span class="dispute-writter"><b>@dt.ReportedBy.FullName</b> on @CreditRegistryRepository.FormatDateTime(dt.TransactionDate)
                wrote:<br />
            </span>
            <div class="dispute-content">
                <div class="dispute-notes">
                    @dt.Notes
                </div>

            </div>
        </div>
    </li>
    }
}