C# 如何在数据库中仅显示5条最新记录-ASP.NET MVC
所以我要做的是显示数据库中存储的5条最新记录,以显示在“Create”视图中。该表存储在“_Recent”中,它是该表的局部视图。该表显示在“创建”视图中,但如何使其仅显示5条最新记录而不是所有内容 \u最近(局部视图)C# 如何在数据库中仅显示5条最新记录-ASP.NET MVC,c#,asp.net,asp.net-mvc,view,C#,Asp.net,Asp.net Mvc,View,所以我要做的是显示数据库中存储的5条最新记录,以显示在“Create”视图中。该表存储在“_Recent”中,它是该表的局部视图。该表显示在“创建”视图中,但如何使其仅显示5条最新记录而不是所有内容 \u最近(局部视图) <div class="col-md-6"> <div class="table-title"> <table class="table-fill"> <thead> <tr&
<div class="col-md-6">
<div class="table-title">
<table class="table-fill">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.TaxBonus)
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
</tr>
</thead>
</div>
@foreach (var item in Model)
{
<tbody class="table-hover">
<tr>
<td clas="text-left">
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Date)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.TaxBonus)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Comment)
</td>
</tr>
</tbody>
}
</table>
public class Charity
{
public int ID { get; set; }
public string DisplayName { get; set; }
public DateTime Date { get; set; }
public Double Amount { get; set; }
public Double TaxBonus { get; set; }
public String Comment { get; set; }
}
public class CharityDBContext : DbContext //controls information in database
{
public DbSet<Charity> Donations { get; set; } //creates a donation database
public Charity Highest { set; get; }
public CharityDBContext()
{
this.Highest = new Charity();
}
}
慈善班
<div class="col-md-6">
<div class="table-title">
<table class="table-fill">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.TaxBonus)
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
</tr>
</thead>
</div>
@foreach (var item in Model)
{
<tbody class="table-hover">
<tr>
<td clas="text-left">
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Date)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.TaxBonus)
</td>
<td class="text-left">
@Html.DisplayFor(modelItem => item.Comment)
</td>
</tr>
</tbody>
}
</table>
public class Charity
{
public int ID { get; set; }
public string DisplayName { get; set; }
public DateTime Date { get; set; }
public Double Amount { get; set; }
public Double TaxBonus { get; set; }
public String Comment { get; set; }
}
public class CharityDBContext : DbContext //controls information in database
{
public DbSet<Charity> Donations { get; set; } //creates a donation database
public Charity Highest { set; get; }
public CharityDBContext()
{
this.Highest = new Charity();
}
}
公益类慈善机构
{
公共int ID{get;set;}
公共字符串DisplayName{get;set;}
公共日期时间日期{get;set;}
公共双倍金额{get;set;}
公共双TaxBonus{get;set;}
公共字符串注释{get;set;}
}
公共类CharityDBContext:DbContext//控制数据库中的信息
{
公共数据库集捐赠{get;set;}//创建捐赠数据库
公共慈善最高{set;get;}
公共慈善背景()
{
这个。最高的=新的慈善机构();
}
}
请检查大写字母,我现在不在visual studio中
已更新
public ActionResult Create()
{
return View(db.Donations.OrderByDescending(x => x.Id).Take(5).ToList());
}
您还可以使用降序排序(x=>x.Date)。好的做法是在创建记录和更新记录时指定日期(列和属性),以便您可以按这些字段排序 正确的方法是:
return视图(db.investments.OrderByDescending(d=>d.ID).Take(5.ToList())代码>
这会使查询在参考底图数据库引擎中执行,并且只从数据库表中获取确切数量的记录。这将先取5条记录,然后将它们反转,但不保证它们会按Id反转。抱歉,我犯了一个错误,.OrderByDescending()在.take(5)之前没有不带参数的OrderByDescending
重载。您始终必须提供Func或lambda。
public ActionResult Create()
{
return View(db.Donations.OrderByDescending(x => x.Id).Take(5).ToList());
}