public PagedList<Log> SearchLogs(LogSearchCriteria criteria) { if (criteria == null) throw new ApplicationException("Search criteria is null."); using (var context = GetContext()) { IQueryable<Log> query = from log in context.Logs join cl in context.CategoryLogs on log.LogId equals cl.LogId join c in context.Categories on cl.CategoryId equals c.CategoryId where c.CategoryName == criteria.Category select log; if (criteria.DateFrom != null) query = query.Where(l => l.TimestampUtc >= criteria.DateFrom); if (criteria.DateTo != null) query = query.Where(l => l.TimestampUtc <= criteria.DateTo); if (!string.IsNullOrEmpty(criteria.Title)) query = query.Where(l => l.Title == criteria.Title); if (criteria.Severity > 0) { List<string> severities = new List<string>(); if ((criteria.Severity & (int)TraceEventType.Critical) != 0) severities.Add(TraceEventType.Critical.ToString()); if ((criteria.Severity & (int)TraceEventType.Error) != 0) severities.Add(TraceEventType.Error.ToString()); if ((criteria.Severity & (int)TraceEventType.Warning) != 0) severities.Add(TraceEventType.Warning.ToString()); if ((criteria.Severity & (int)TraceEventType.Information) != 0) severities.Add(TraceEventType.Information.ToString()); query = query.Where(l => severities.Contains(l.SeverityName)); } query = query.SortBy(criteria.SortBy, criteria.SortByDesc); PagedList<int> logViewIds = new PagedList<int>(query.Select(l => l.LogId), criteria.StartIndex, criteria.PageSize); IQueryable<Log> secondQuery = context.Logs.Where(l => logViewIds.Contains(l.LogId)); Dictionary<int, Log> logs = (criteria.ShouldIncludeMessage ? secondQuery.Select(l => new LogToView { LogId = l.LogId, TimestampUtc = l.TimestampUtc, Message = l.FormattedMessage }) : secondQuery.Select(l => new LogToView { LogId = l.LogId, SeverityName = l.SeverityName, Title = l.Title, TimestampUtc = l.TimestampUtc, Message = l.Message.Substring(0, criteria.MessageLength) })).ToDictionary(l => l.LogId, l => (Log)l); return logViewIds.Transform<Log>(id => logs[id]); } }
public PagedList<KeyExportLog> SearchExportLogs(ExportLogSearchCriteria criteria) { using (var context = GetContext()) { var query = context.KeyExportLogs.Where(log => log.ExportLogId >= 0); if (criteria != null) { if (criteria.DateFrom != null) query = query.Where(k => k.CreateDate >= criteria.DateFrom); if (criteria.DateTo != null) query = query.Where(k => k.CreateDate <= criteria.DateTo); if (!string.IsNullOrEmpty(criteria.ExportTo)) query = query.Where(k => k.ExportTo.Contains(criteria.ExportTo)); if (criteria.ExportTypes != null && criteria.ExportTypes.Count() > 0) query = query.Where(k => criteria.ExportTypes.Contains(k.ExportType)); if (!string.IsNullOrEmpty(criteria.CreateBy)) query = query.Where(k => k.CreateBy == criteria.CreateBy); if (criteria.IsEncrypted != null && criteria.IsEncrypted.HasValue) query = query.Where(k => k.IsEncrypted == criteria.IsEncrypted); if (!string.IsNullOrEmpty(criteria.FileName)) query = query.Where(k => k.FileName.Contains(criteria.FileName)); query = query.SortBy(criteria.SortBy, criteria.SortByDesc); } PagedList<int> logIds = new PagedList<int>(query.Select(l => l.ExportLogId), criteria.StartIndex, criteria.PageSize); Dictionary<int, KeyExportLog> dic = context.KeyExportLogs .Where(l => logIds.Contains(l.ExportLogId)).ToDictionary(l => l.ExportLogId, l => l); return logIds.Transform(id => dic[id]); } }