[英]Showing First Instance and Last Instance of GroupBy ASP.NET MVC
I have a dashboard that shows an updated list of application errors. 我有一个仪表板,显示应用程序错误的更新列表。 Some errors have happened more than once so I have grouped them by their error type and placed a count on them to see how many times this error has occurred. 有些错误已经发生了不止一次,因此我按错误类型对它们进行了分组,并对它们进行计数,以查看发生此错误的次数。 I am wanting to find out when the first date the error occurred, which is fairly straight forward, but also the last date it occurred too to see how long this error has been persisting. 我想找出错误发生的第一个日期,这很简单,但是也要找出发生错误的最后日期,以查看此错误持续了多长时间。
Below I show both my current output and expected output 下面我同时显示我的当前输出和预期输出
Below I show my code. 下面我显示我的代码。 What changes would I need to make in order to display the last date as well as the first date? 为了显示最后日期和第一个日期,我需要进行哪些更改? I have tried using LastOrDefault()
but this doesn't actually work as when the LINQ query is converted into a SQL statement, there is no BOTTOM 1
command. 我曾尝试使用LastOrDefault()
但这实际上不起作用,因为当LINQ查询转换为SQL语句时,没有BOTTOM 1
命令。
public ActionResult Errors(string sortOrder, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
var queryString = RouteData.Values["id"];
var applications = db.ElmahErrors.Where(s => s.Application.Replace("/", "").Replace(".", "") == queryString)
.GroupBy(s => s.Type)
.Select(grp => new ErrorCountModel
{
ErrorCount = grp.Count(),
ElmahError = grp.FirstOrDefault()
});
switch (sortOrder)
{
default:
applications = applications.OrderBy(s => s.ElmahError.TimeUtc);
break;
}
int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
int pageNumber = (page ?? 1);
return View(applications.ToPagedList(pageNumber, pageSize));
}
public class ErrorCountModel
{
public int ErrorCount { get; set; }
public ElmahError ElmahError { get; set; }
}
Any help on this matter would be greatly appreciated. 在此问题上的任何帮助将不胜感激。
Thanks in advance. 提前致谢。
Side note: The following is not guaranteed to give you the newest error, because you haven't told it to sort 旁注:不能保证以下内容会为您提供最新的错误,因为您尚未告诉它进行排序
ElmahError = grp.FirstOrDefault()
It should be this: 应该是这样的:
ElmahError = grp.OrderBy(s => s.ElmahError.TimeUtc).FirstOrDefault();
I would have expected .LastOrDefault() to work, but if it really doesn't, you can do this: 我本以为.LastOrDefault()可以工作,但是如果确实不能,则可以执行以下操作:
LastElmahError = grp.OrderByDescending(s => s.ElmahError.TimeUtc).FirstOrDefault();
Essentially, you reverse the ordering of the first error. 本质上,您可以颠倒第一个错误的顺序。
Also, add this to your ErrorCountModel
model class: 另外,将其添加到您的ErrorCountModel
模型类中:
public ElmahError LastElmahError { get; set; }
Another way to do this would have been to create a view model as follows: 执行此操作的另一种方法是创建视图模型,如下所示:
public class ElmahErrorViewModel {
public Guid ID {get;set;}
public string Application {get;set;}
public string Host {get;set;}
public string Type {get;set;}
public int ErrorCount {get;set;}
public DateTime Min {get;set;}
public DateTime Max {get;set;}
}
And then just: 然后:
var applications = db.ElmahErrors.Where(s => s.Application.Replace("/", "").Replace(".", "") == queryString)
.GroupBy(s => new { s.Type, s.ID, s.Application, s.Host })
.Select(grp => new ElmahErrorViewModel {
ID = grp.Key.ID,
Application = grp.Key.Application,
Host = grp.Key.Host,
Type = grp.Key.Type,
ErrorCount = grp.Count(),
Min = grp.Min(m => m.TimeUtc),
Max = grp.Max(m => m.TimeUtc),
});
The idea being that this returns a flattened model ready to go whereas I assume with a LastElmahError
property you still have to flatten it at some point to display it in the UI and the only field you actually want out of that is TimeUtc
so most of it is redundant. 想法是,这将返回准备好进行展平的模型,而我假设使用LastElmahError
属性,您仍必须在某个点展平它才能在UI中显示它,而您真正想要的唯一字段是TimeUtc
因此大部分是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.