我目前显示数据库中的错误列表。从下图中可以看出,有许多错误共享相同的应用程序名称。
我想将所有同名的错误组合在一起,只显示该应用程序名称的一个错误,而不是全部错误。
我在这里看到了与我正在做的事情有关的其他问题,但没有人给我答案。
这是我目前的代码。
public ActionResult Index(string sortOrder, string searchString, string currentFilter, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var applications = from s in db.ElmahErrors
select s;
if (!String.IsNullOrEmpty(searchString))
{
applications = applications.Where(s => s.Application.Contains(searchString));
}
switch (sortOrder)
{
default:
applications = applications.OrderBy(x => x.Application).ThenByDescending(s => s.TimeUtc);
break;
}
int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
int pageNumber = (page ?? 1);
return View(applications.ToPagedList(pageNumber, pageSize));
}
@model PagedList.IPagedList<DataIntelligence.Models.ElmahError>
@using PagedList.Mvc;
<link href="Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Application Error Dashboard";
}
<div class="jumbotron">
<h2>Application Error Dashboard</h2>
</div>
@using (Html.BeginForm("Index", "Application", FormMethod.Get))
{
<p>
Find by Application: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table>
<tr>
<th>
Application
</th>
<th>
Host
</th>
<th>
Type
</th>
<th>
Date
</th>
<th>
Message
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
<a href="@Url.Action("Details", new { id=item.ErrorId})"> @Html.DisplayFor(modelItem => item.Application) </a>
</td>
<td>
@Html.DisplayFor(modelItem => item.Host)
</td>
<td>
@Html.DisplayFor(modelItem => item.Type)
</td>
<td >
@Html.DisplayFor(modelItem => item.TimeUtc)
</td>
<td>
@Html.DisplayFor(modelItem => item.Message)
</td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
如何更改此代码以实现我想要的目标?
提前致谢。
答案 0 :(得分:1)
如果替换:
var applications = from s in db.ElmahErrors
select s;
使用:
var applications = db.ElmahErrors
.GroupBy(s => s.Application)
.Select(grp => grp.FirstOrDefault());
然后查询将只处理每个应用程序的一个元素。
您可能还希望在以后的状态下执行此分组,如果ElmahErrors
中有某些元素要根据其他条件过滤掉,以确保您没有碰巧过滤掉不同的当另一个具有相同Application
的项目未被过滤时选择的项目。
答案 1 :(得分:0)
您应该查看“MoreLinq&#39;”,也许他们的DistinctBy会提供您需要的答案。它在NuGet中可用。 MoreLINQ's DistinctBy for C#
应该是这样的:
List<ElmahErrors> distinctByError = db.ElmahErrors.DistinctBy(x => x.Message).ToList();
答案 2 :(得分:0)
尝试此查询
var applications = from s in db.ElmahErrors
group c by new
{
s.Application,
s.Host,
s.Type,
s.Date,
s.Message
} into gcs
select new ElmahErrors()
{
gcs.key.Application,
gcs.key.Host,
gcs.key.Type,
gcs.key.Date,
gcs.key.Message
};
答案 3 :(得分:0)
var results = from a in applications
group a by a.Application into g
select new { Application = g.Key, Count = g.Count(), LastDate = g.Max(d=>d.Date) };
或者,如果你想通过更多的电影分组:
var results = from a in applications
group a by new {a.Application, a.Message} into g
select new { Application = g.Key.Application, Message = g.Key.Message, Count = g.Count(), LastDate = g.Max(d=>d.Date) };