private IPagedList<Inspect> QueryInspect(InspectQueryModel query, int? currentPageIndex, int? pageSize)
 {
     IPagedList<Inspect> inspects;
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         IInspectRep inspectRep = new InspectRep(unitOfWork);
         inspects = GenerateQuery(unitOfWork, query).ToPagedList(currentPageIndex.Value, pageSize.Value);
     }
     return inspects;
 }
 private IQueryable<Inspect> GenerateQuery(IUnitOfWork unitOfWork, InspectQueryModel query)
 {
     IInspectRep inspectRep = new InspectRep(unitOfWork);
     return inspectRep.Queryable().Where(item => (string.IsNullOrEmpty(query.TskNo) ? true : item.TskNo.Contains(query.TskNo))
         && (string.IsNullOrEmpty(query.LeoniNo) ? true : item.LeoniNo.Contains(query.LeoniNo))
         && (string.IsNullOrEmpty(query.CusNo) ? true : item.CusNo.Contains(query.CusNo))
         && (string.IsNullOrEmpty(query.ClipScanNo) ? true : item.ClipScanNo.Contains(query.ClipScanNo))
         && ((query.ClipScanTime1Start.HasValue) ? item.ClipScanTime1 >= query.ClipScanTime1Start : true)
         && ((query.ClipScanTime1End.HasValue) ? item.ClipScanTime1 <= query.ClipScanTime1End : true)
         && ((query.ClipScanTime2Start.HasValue) ? item.ClipScanTime2 >= query.ClipScanTime2Start : true)
         && ((query.ClipScanTime2End.HasValue) ? item.ClipScanTime2 <= query.ClipScanTime2End : true)
         && (string.IsNullOrEmpty(query.TskScanNo) ? true : item.TskScanNo.Contains(query.TskScanNo))
         && ((query.TskScanTime3Start.HasValue) ? item.TskScanTime3 >= query.TskScanTime3Start : true)
         && ((query.TskScanTime3End.HasValue) ? item.TskScanTime3 <= query.TskScanTime3End : true)
         && ((query.Time3MinTime2Start.HasValue) ? item.Time3MinTime2 >= query.Time3MinTime2Start : true)
         && ((query.Time3MinTime2End.HasValue) ? item.Time3MinTime2 <= query.Time3MinTime2End : true)
         && (query.CreatedAtStart.HasValue ? item.CreatedAt >= query.CreatedAtStart : true)
         && (query.CreatedAtEnd.HasValue ? item.CreatedAt <= query.CreatedAtEnd : true)
         && (string.IsNullOrEmpty(query.OkOrNot) ? true : item.OkOrNot.Contains(query.OkOrNot))
         );
 }
 private List<Inspect> ExportInspect(InspectQueryModel query)
 {
     List<Inspect> inspects = new List<Inspect>();
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         IInspectRep inspectRep = new InspectRep(unitOfWork);
         inspects = GenerateQuery(unitOfWork, query).ToList<Inspect>();
     }
     return inspects;
 }
 public ActionResult Query()
 {
     InspectQueryModel query = new InspectQueryModel(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", QueryInspect(query, currentPageIndex, pageSize));
 }
        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("ClipScanTime1Start", endTime.AddDays(-1).ToString("yyyy/MM/dd 10:00"));
            q.Add("ClipScanTime1End", DateTime.Now.AddHours(1).ToString("yyyy/MM/dd HH:00"));
            InspectQueryModel query = new InspectQueryModel(q);

            IPagedList<Inspect> inspects = null;
            using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
            {
                int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
                IInspectRep inspectRep = new InspectRep(unitOfWork);
                inspects = inspectRep.Queryable(query.ClipScanTime1Start,query.ClipScanTime1End).ToPagedList(currentPageIndex, int.Parse(Resources.PageSize));
            }
            ViewBag.Query = query;
            return View(inspects);
        }
 public void Export()
 {
     InspectQueryModel query = new InspectQueryModel(Request.QueryString);
     ViewBag.Query = query;
     List<Inspect> inspects = ExportInspect(query);
     MemoryStream ms = new MemoryStream();
     using (StreamWriter sw = new StreamWriter(ms, Encoding.UTF8))
     {
         // write head
         //   string max =
         sw.WriteLine(string.Join(",", InspectQueryModel.CsvHead.ToArray()));
         foreach (Inspect i in inspects)
         {
             List<string> ii = new List<string>();
             foreach (string field in InspectQueryModel.Fileds)
             {
                 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 = "Inspect" + 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();
 }