public ActionResult Index(int? page) { DateTime dt = DateTime.Now; DateTime endTime = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); NameValueCollection q = new NameValueCollection(); q.Add("CreatedAtStart", endTime.AddDays(-1).ToString("yyyy/MM/dd 10:00")); q.Add("CreatedAtEnd", DateTime.Now.AddHours(1).ToString("yyyy/MM/dd HH:00")); InspectOriginQueryModel query = new InspectOriginQueryModel(q); IPagedList<InspectOrigin> inspects = null; using (var txn = new System.Transactions.TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted })) { using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString)) { int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0; IInspectOriginRep inspectOriginRep = new InspectOriginRep(unitOfWork); inspects = inspectOriginRep.Queryable(query.CreatedAtStart,query.CreatedAtEnd).ToPagedList(currentPageIndex, int.Parse(Resources.PageSize)); } } ViewBag.Query = query; return View(inspects); }
public void Export() { InspectOriginQueryModel query = new InspectOriginQueryModel(Request.QueryString); ViewBag.Query = query; List<InspectOrigin> inspects = ExportInspectOrigin(query); MemoryStream ms = new MemoryStream(); using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8)) { // write head // string max = sw.WriteLine(string.Join(",", InspectOriginQueryModel.CsvHead.ToArray())); foreach (InspectOrigin i in inspects) { List<string> ii = new List<string>(); foreach (string field in InspectOriginQueryModel.Fileds) { // var p = i.GetType().GetProperties(); var value= i.GetType().GetProperty(field).GetValue(i, null); ii.Add(value==null ? "" : value.ToString()); } sw.WriteLine(string.Join(",", ii.ToArray())); } //sw.WriteLine(max); } var filename = "InspectOrigin" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv"; var contenttype = "text/csv"; Response.Clear(); Response.ContentEncoding = Encoding.UTF8; Response.ContentType = contenttype; Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(ms.ToArray()); Response.End(); }
private IPagedList<InspectOrigin> QueryInspectOrigin(InspectOriginQueryModel query, int? currentPageIndex, int? pageSize) { IPagedList<InspectOrigin> inspects; using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString)) { IInspectOriginRep inspectRep = new InspectOriginRep(unitOfWork); inspects = GenerateQuery(unitOfWork, query).ToPagedList(currentPageIndex.Value, pageSize.Value); } return inspects; }
private IQueryable<InspectOrigin> GenerateQuery(IUnitOfWork unitOfWork, InspectOriginQueryModel query) { IInspectOriginRep inspectRep = new InspectOriginRep(unitOfWork); return inspectRep.Queryable().Where(item => //(string.IsNullOrEmpty(query.Text) ? true : item.Text.Contains(query.Text))&& (query.ProcessResult.HasValue ? item.ProcessResult.Equals(query.ProcessResult) : true) && (query.CreatedAtStart.HasValue ? item.CreatedAt >= query.CreatedAtStart : true) && (query.CreatedAtEnd.HasValue ? item.CreatedAt <= query.CreatedAtEnd : true) ); }
private List<InspectOrigin> ExportInspectOrigin(InspectOriginQueryModel query) { List<InspectOrigin> inspects = new List<InspectOrigin>(); using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString)) { IInspectOriginRep inspectRep = new InspectOriginRep(unitOfWork); inspects = GenerateQuery(unitOfWork, query).ToList<InspectOrigin>(); } return inspects; }
public ActionResult Query() { InspectOriginQueryModel query = new InspectOriginQueryModel(Request.QueryString); int currentPageIndex = 0; int.TryParse(Request.QueryString.Get("page"), out currentPageIndex); currentPageIndex = currentPageIndex <= 0 ? 0 : currentPageIndex - 1; int pageSize = int.Parse(Resources.PageSize); ViewBag.Query = query; return View("Index", QueryInspectOrigin(query, currentPageIndex, pageSize)); }