为什么我的姓名和日期排序不起作用。当我单击列链接时,我的数据没有任何反应,也没有错误。当我运行调试并逐步完成排序时,它会选择正确的switch语句,然后生成相同的数据。
我不确定为什么它会给我相同的数据。我使用了所有匹配的表名。
控制器:
// GET: Managerial
public async Task<IActionResult> Index(string sortOrder)
{
//QUERY
ViewBag.FullName = UserInformation.Globals.FullName;
var ParentORG = _context.CORP_MatrixPositionOLDWay
.Where(p => p.ParentLAN == UserInformation.Globals.LANID)
.Select(p => p.ParentOrgLevel.TrimEnd('!'))
.First();
var LANlist = _context.CORP_MatrixPositionOLDWay
.Where(x => x.ChildOrgLevel.StartsWith(ParentORG.ToString()))
.Select(x => x.ChildLAN)
.Where(lan =>lan != UserInformation.Globals.LANID);
var certificationContext = _context.INT_CertificationsXREF
.Include(i => i.INT_CertificationCategories)
.Include(i => i.INT_Certifications)
.Include(i => i.INT_CertificationConferred)
.Include(i => i.RIM_Resource)
.Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN)
.Where(i => LANlist.Contains(i.RIM_Resource.LAN));
//Sorting
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
var resources = from s in _context.INT_CertificationsXREF
select s;
switch (sortOrder)
{
case "name_desc":
resources = resources.OrderByDescending(s =>
s.RIM_Resource.LastName);
break;
case "Date":
resources = resources.OrderBy(s => s.RenewDate);
break;
case "date_desc":
resources = resources.OrderByDescending(s => s.RenewDate);
break;
default:
resources = resources.OrderBy(s => s.RIM_Resource.LastName);
break;
}
return View(await certificationContext.AsNoTracking().ToListAsync());
}
查看:
@model IEnumerable<Certifications.Models.INT_CertificationsXREF>
<link href="~/css/Overide.css" rel="stylesheet" />
@{
ViewData["Title"] = "Index";
}
<table class="table CDS_Table" Id="myTable">
<thead>
<tr>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData
["NameSortParm"]">@Html.DisplayNameFor(model => model.RIM_Resource)</a>
</th>
<th>
@Html.DisplayNameFor(model => model.INT_Certifications)
</th>
<th>
@Html.DisplayNameFor(model => model.INT_Certifications.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.INT_CertificationCategories.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.INT_CertificationConferred.ConferredBy)
</th>
<th>
@Html.DisplayNameFor(model => model.AwardDate)
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]">@Html.DisplayNameFor(model => model.RenewDate)</a>
</th>
<th>
@Html.DisplayNameFor(model => model.Approved)
</th>
<th>
@Html.DisplayNameFor(model => model.IsActive)
</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
<a asp-action="Edit" asp-route-id="@item.ID"> @Html.DisplayFor(modelItem => item.RIM_Resource.FirstName) @Html.DisplayFor(modelItem => item.RIM_Resource.LastName) </a>
</td>
<td>
@Html.DisplayFor(modelItem => item.INT_Certifications.Certification)
</td>
<td>
@Html.DisplayFor(modelItem => item.INT_Certifications.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.INT_CertificationCategories.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.INT_CertificationConferred.ConferredBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.AwardDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.RenewDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Approved)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsActive)
</td>
<td>
<form asp-action="Index">
<div class="form-actions no-color">
@if (item.Approved == true)
{
<input type="submit" value="Revoke" class="btn btn-default" asp-route-id="@item.ID" />
}
else
{
<input type="submit" value="Approve" class="btn btn-default" asp-route-id="@item.ID" />
}
</div>
</form>
</td>
<td>
<a asp-action="Delete" asp-route-id="@item.ID">Deactivate</a>
</td>
</tr>
}
</tbody>
</table>
答案 0 :(得分:1)
首先需要知道的是,有2个LINQ查询具有相同的数据上下文源但使用不同的语法:
首次质询
// SELECT ... FROM ... WHERE ... with JOIN
var certificationContext = _context.INT_CertificationsXREF
.Include(i => i.INT_CertificationCategories)
.Include(i => i.INT_Certifications)
.Include(i => i.INT_CertificationConferred)
.Include(i => i.RIM_Resource)
.Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN)
.Where(i => LANlist.Contains(i.RIM_Resource.LAN));
第二次查询
// equivalent: SELECT * FROM INT_CertificationsXREF
var resources = from s in _context.INT_CertificationsXREF
select s;
下面的switch语句执行顺利,但似乎是对第二个查询而不是第一个查询的结果进行排序:
switch (sortOrder)
{
case "name_desc":
resources = resources.OrderByDescending(s => s.RIM_Resource.LastName);
break;
case "Date":
resources = resources.OrderBy(s => s.RenewDate);
break;
case "date_desc":
resources = resources.OrderByDescending(s => s.RenewDate);
break;
default:
resources = resources.OrderBy(s => s.RIM_Resource.LastName);
break;
}
您返回查看页面的内容是来自第一个查询的模型,该查询未排序:
return View(await certificationContext.AsNoTracking().ToListAsync());
您需要从以下两个选项中选择一个:
1)将switch语句更改为对第一个查询进行排序,而不将返回的模型更改为查看页面:
switch (sortOrder)
{
case "name_desc":
certificationContext = certificationContext.OrderByDescending(s => s.RIM_Resource.LastName);
break;
case "Date":
certificationContext = certificationContext.OrderBy(s => s.RenewDate);
break;
case "date_desc":
certificationContext = certificationContext.OrderByDescending(s => s.RenewDate);
break;
default:
certificationContext = certificationContext.OrderBy(s => s.RIM_Resource.LastName);
break;
}
2)更改模型返回类型以将已排序的第二个查询传递到视图页面:
return View(await resources.AsNoTracking().ToListAsync());
上面的这2个选项根据您要使用的选项有不同的查询结果。