消息回答由父原始消息分组

时间:2013-06-13 15:01:48

标签: c# asp.net-mvc linq

我需要通过Messages Answers对邮件进行分组,如下所示:

  • 消息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>
}

1 个答案:

答案 0 :(得分:6)

var model=Messages
             .OrderBy(o=>o.MessageParent_Id==0?Id:o.MessageParent_Id)
             .ThenBy(o=>o.Id);