1

我需要按消息答案对消息进行分组,如下所示:

  • 消息 ID = 1
  • 消息答案 id = 2 / 父消息 id = 1
  • 消息答案 id = 3 / 父消息 id = 1
  • 消息答案 id = 4 / 父消息 id = 1

  • 消息 ID = 5

  • 消息答案 id = 6 / 父消息 id = 5
  • 消息答案 id = 7 / 父消息 id = 5
  • 消息答案 id = 8 / 父消息 id = 5

消息模型:

public class Message
{
    // Primary properties
    public int Id { get; set; }

    public string NameFrom { get; set; }
    public string EmailFrom { get; set; }
    public string TelephoneFrom { get; set; }

    public string NameTo { get; set; }
    public string EmailTo { get; set; }
    public string TelephoneTo { get; set; }

    public string Title { get; set; }
    public string MessageText { get; set; }
    public bool? Approved { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DateRead { get; set; }

    // Navigation properties
    public Member MemberFrom { get; set; }
    public Member MemberTo { get; set; }
    public MessageType MessageType { get; set; }
    public Message MessageParent { get; set; }

    public Ad Ad { get; set; }
    public Ask Ask { get; set; }
}

要在视图中呈现它:

@foreach (var item in Model)
{
    <div class="message_link_container" id="linkmessage_@item.Id">
        <ul class="estr_message" id="estrmessage_@item.Id">
            <li id="email_@item.Id">
                <p>@if(item.MessageParent_Id == 0){ if(string.IsNullOrEmpty(item.NameFrom)) { @item.EmailFrom } else { @item.NameFrom } }</p>
            </li>
            <li id="title_@item.Id">
                <p>@item.Title</p>
            </li>
            <li id="date_@item.Id">
                <p>@item.DateCreated</p>
            </li>
        </ul>
    </div>
}
4

1 回答 1

6
var model=Messages
             .OrderBy(o=>o.MessageParent_Id==0?Id:o.MessageParent_Id)
             .ThenBy(o=>o.Id);
于 2013-06-18T21:24:46.340 回答