列出最近5条记录

时间:2014-05-03 11:09:02

标签: c# asp.net-mvc list date

我正在尝试列出最后5个记录,直到最近的约会(最近的约会)。这是我的控制器运行良好,但我怎么能过滤它只有5个记录,哪个是今天之前的日期。

  using (var db = new MaindbModelDataContext())
            {
                var patient = db.Patients.FirstOrDefault(u => u.Email == (String)Session["UserEmail"]);
                var listrecent = from y in db.Appointments
                                 where y.PatientNo == patient.PatientNo
                                 select y;
                var TempRecent = new List<Models.AppModel>();
                foreach (var item in listrecent)
                {
                    var Temp = new Models.AppModel();
                    Temp.AppNo = item.AppNo;
                    Temp.PatientNo = (Int32)item.PatientNo;
                    Temp.Date = (DateTime)item.Date;
                    Temp.Status = item.Status;
                    Temp.Description = item.Description;
                    TempRecent.Add(Temp);

                }
                return View(TempRecent);
            }

        }
        else
        {
            return RedirectToAction("RegAndLogin", "User");
        }
    }

}

这是我的观点

@model IEnumerable<DentAppSys.Models.AppModel>
@using System.Web.Helpers
 <section class="Patient-Dashboard">

        <div id="dashboard_left">
            <h1> Recent Appointments</h1>
   @{
    var Mygrid = new WebGrid(Model, selectionFieldName: "SelectedRow");
}

@Mygrid.GetHtml(
    displayHeader: true,
    alternatingRowStyle: "alt",
    selectedRowStyle: "select",
    mode: WebGridPagerModes.All,

columns: Mygrid.Columns
(

    Mygrid.Column("Appointment No", "Appointment No",format: @<text>@item.AppNo</text>),
    Mygrid.Column("Patient No", "Patient No", format: @<text>@item.PatientNo</text>) ,
    Mygrid.Column("Description", "Description", format: @<text>@item.Description</text>),
    Mygrid.Column("Date", "Date", format: @<text>@item.Date.ToString("MM/dd/yyyy")</text>),
    Mygrid.Column("Status", "Status", format: @<text>@item.Status</text>)

))
</div>

<div id="dashboard_right">
<br/>
<h1>Incoming Appointments</h1>
</div>
</section>

1 个答案:

答案 0 :(得分:1)

我相信你的linq语句中的这样的东西应该有效:

var listrecent = (from y in db.Appointments
                 where y.PatientNo == patient.PatientNo
                 where y.Date < DateTime.Today
                 orderby y.Date descending
                 select y).Take(5);