我目前有一个linq查询,它从我的视图中的日期输入中获取值。
var applications = from s in db.ElmahErrors
where s.TimeUtc >= startDate && s.TimeUtc < endDate
select s;
因此,此查询显示在输入中选择的那一天发生的每个错误。
许多错误是具有相同应用程序名称和类型的重复出现的错误。我想按应用程序名称和类型对这些错误进行分组,因此它只显示每个错误的一行。
我已经给了它一个去,但它似乎不喜欢它。
var applications = from s in db.ElmahErrors
where s.TimeUtc >= startDate && s.TimeUtc < endDate
group s by new {s.Application, s.Type } into g
select new ElmahError
{
Application = g.Key,
Type = g.Key,
ErrorCount = g.Count()
};
[Table("ELMAH_Error")]
public class ElmahError
{
[Key]
public System.Guid ErrorId { get; set; }
public System.String Application { get; set; }
public System.String Host { get; set; }
public System.String Type { get; set; }
public System.String Source { get; set; }
public System.String Message { get; set; }
public System.String User { get; set; }
public System.Int32 StatusCode { get; set; }
public System.DateTime TimeUtc { get; set; }
public System.Int32 Sequence { get; set; }
public System.String AllXml { get; set; }
public int ErrorCount { get; set; }
}
任何帮助都将不胜感激。
提前致谢。
答案 0 :(得分:0)
我在这里猜你的字段/属性名称,但这应该有效:
var applications = from s in db.ElmahErrors
where s.TimeUtc >= startDate && s.TimeUtc < endDate
group by new { s.Application, s.Type } into grp
select grp;
答案 1 :(得分:0)
您需要的只是group by条款: -
var ErrorsByapplications = from s in db.ElmahErrors
where s.TimeUtc >= startDate && s.TimeUtc < endDate
group s by new {s.ApplicationName, s.Type } into g
select new
{
ApplicationName = g.Key,
Type = g.Key,
TotalErrors = g.Count()
...and so on
};
<强>更新强>
如果您的ElmahError
类已经拥有ApplicationName
,Type
等属性,那么您可以投影: -
group s by new {s.ApplicationName, s.Type } into g
select new ElmahError
{
ApplicationName = g.Key,
Type = g.Key,
TotalErrors = g.Count() //If you have such property
ErrorMessage = f.First().Errormessage //This will display only first
//error out of all (Consider checking for nulls)
};
答案 2 :(得分:0)
试试这个:
var ErrorsByapplications = from s in db.ElmahErrors
where s.TimeUtc >= startDate && s.TimeUtc < endDate
group s by new {s.Application, s.Type } into g
select new
{
ApplicationName = g.Key,
Error = g.ToList()
};