我有一个包含日志的表,我每天计算的日志如下:
// Count logs by day
IList<DataModel> models = _context.Logs
.Where(x => x.Created >= dateMinimum && x.Created <= dateMaximum)
.GroupBy(x => new { Year = x.Created.Year, Month = x.Created.Month, Day = x.Created.Day })
.Select(x => new { Year = x.Key.Year, Month = x.Key.Month, Day = x.Key.Day, Count = x.Count() })
.AsEnumerable()
.Select(x => new DataModel { Date = new DateTime(x.Year, x.Month, x.Day), LogsCount = x.Count })
.ToList();
// Fill empty days with dates which contains all days in range
models.AddRange(dates.Where(x => !models.Any(y => y.Date == x.Date)).Select(x => new DataModel { Date = x, LogsCount = 0 }));
如果我想独立于类型按天计算所有日志,这是可行的。
但我想按天计算日志并输入(错误、警告、信息……)。
我试图将 x.Type 添加到组中,但最后我只得到 3 个项目。
目前我的数据模型如下:
public class DataModel
{
public DateTime Date { get; set; }
public Int32 LogsCount { get; set; }
}
但也许它应该是这样的:
public class DataModel
{
public DateTime Date { get; set; }
public KeyValuePair<String, Int32> LogsCount { get; set; }
}
其中 LogsCount 有一个包含类型的字符串和包含计数的 Int32。
我怎样才能做到这一点?