From 8846519bba47a21637458695e4865901a225fa7a Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 17 Mar 2019 16:18:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=BC=82=E5=B8=B8=E4=BD=BF=E7=94=A8MongoDB?= =?UTF-8?q?=E7=9A=84Find=E4=B8=8ESort?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.DataAccess.MongoDB/Exceptions.cs | 32 ++++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Bootstrap.DataAccess.MongoDB/Exceptions.cs b/Bootstrap.DataAccess.MongoDB/Exceptions.cs index fe8d2200..12f1f140 100644 --- a/Bootstrap.DataAccess.MongoDB/Exceptions.cs +++ b/Bootstrap.DataAccess.MongoDB/Exceptions.cs @@ -66,31 +66,33 @@ namespace Bootstrap.DataAccess.MongoDB /// public override Page RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime) { - var exceps = DbManager.Exceptions.Find(FilterDefinition.Empty).ToList(); + // filter + var filterBuilder = Builders.Filter; + var filter = filterBuilder.Empty; + if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value); + if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1)); + if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddDays(-7)); + // sort - var orderProxy = po.Order == "asc" ? - new Func, List>(p => exceps.OrderBy(p).ToList()) : - new Func, List>(p => exceps.OrderByDescending(p).ToList()); - - var logTimeProxy = po.Order == "asc" ? - new Func, List>(p => exceps.OrderBy(p).ToList()) : - new Func, List>(p => exceps.OrderByDescending(p).ToList()); - + var sortBuilder = Builders.Sort; + SortDefinition sort = null; switch (po.Sort) { + case "LogTime": + sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime); + break; case "ErrorPage": - exceps = orderProxy(ex => ex.ErrorPage); + sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.ErrorPage) : sortBuilder.Descending(t => t.ErrorPage); break; case "UserId": - exceps = orderProxy(ex => ex.UserId); + sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserId) : sortBuilder.Descending(t => t.UserId); break; case "UserIp": - exceps = orderProxy(ex => ex.UserIp); - break; - case "LogTime": - exceps = logTimeProxy(ex => ex.LogTime); + sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserIp) : sortBuilder.Descending(t => t.UserIp); break; } + + var exceps = DbManager.Exceptions.Find(filter).Sort(sort).ToList(); return new Page() { Context = exceps,