简体   繁体   English

显示ASP.NET MVC的GroupB的第一个实例和最后一个实例

[英]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 下面我同时显示我的当前输出和预期输出

Current Output 电流输出

在此处输入图片说明

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命令。

Controller 调节器

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));
}

ErrorCount Model 错误计数模型

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.

相关问题 数据库查询仅返回数据库的第一个元素作为所有相同的实例,在 ASP.NET MVC Core 6 - Database query returning only the first element of the data base as the same instance for all, in ASP.NET MVC Core 6 从ASP.NET MVC中的ModelMetadataProvider获取包含对象实例 - Obtain containing object instance from ModelMetadataProvider in ASP.NET MVC ASP.NET MVC:视图中的访问控制器实例 - ASP.NET MVC: Access controller instance from view Asp.Net MVC4'对象引用未设置为对象的实例' - Asp.Net MVC4 'Object reference not set to an instance of an object' Asp.net MVC4,C#创建对象实例 - Asp.net MVC4, C# Create object instance ASP.NET Core MVC 2:服务器端的NameValueCollection实例为空 - ASP.NET Core MVC 2: NameValueCollection instance is empty on the server side ASP.NET MVC对象引用未设置为对象的实例 - ASP.NET MVC Object reference not set to an instance of an object 从adlds实例验证asp.net mvc 5应用程序 - authentificate asp.net mvc 5 application from adlds instance ASP.Net MVC-“无法创建接口实例” - ASP.Net MVC - “Cannot Create Instance of an Interface” ASP.NET MVC 3应用程序无法连接到AWS实例上的SQL Server 2008实例 - ASP.NET MVC 3 app cannot connect to SQL server 2008 instance on AWS instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM