Once upon a time, there was such a problem in the analysis of the purchase, sale and stock of goods, including the purchase quantity, sales quantity and stock quantity, which were not carried out or sold in the same day, but the stock quantity remained unchanged; as shown in the following figure:
At the beginning of writing the time judgment, there should be no data in this time range. The returned data should be 0, which is almost the situation of clearing the table data. After thinking carefully, I will judge the number of data. If the data is greater than one, I will return the original value. If it is 0, I will copy the previous query again, and assign the purchase quantity and sales quantity Is 0, the inventory number is reserved.
The code is as follows:
public ActionResult SelectJinXiaoCun(BsgridPage bsgridPage, string start, string end) { //Two time parameters start,end; var list = (from tbXSMX in myModels.Base_XiaoShoMX join tbXS in myModels.Base_XiaoShouDan on tbXSMX.XiaoShouDanID equals tbXS.XiaoShouDanID join tbKuCun in myModels.JC_KuCun on tbXSMX.KuCunID equals tbKuCun.KuCunID from tbJinHuoMX in myModels.Base_JinHuoMX where tbJinHuoMX.KuCunID == tbXSMX.KuCunID select new JinHuoVo { XiaoShoMXID = tbXSMX.XiaoShoMXID, XiaoShouDate = tbXS.XiaoShouDate, XiaoJi = tbXSMX.XiaoJi, KuCunNumber = tbKuCun.KuCunNumber, ShuLiang = tbJinHuoMX.ShuLiang, XiaoShouNumber = tbXSMX.XiaoShouNumber }).ToList(); if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end)) //Filter data by time condition { DateTime Start = Convert.ToDateTime(start); DateTime End = Convert.ToDateTime(end); list = list.Where(m => m.XiaoShouDate >= Start && m.XiaoShouDate <= End).ToList(); } var lists = (from tb in list group tb by tb.XiaoJi into JX select new JinHuoVo { XiaoJi = JX.OrderByDescending(m => m.XiaoShoMXID).FirstOrDefault().XiaoJi, KuCunNumber = JX.Sum(m => m.KuCunNumber), ShuLiang = JX.Sum(m => m.ShuLiang), XiaoShouNumber = JX.Sum(m => m.XiaoShouNumber) }).ToList();//Data grouping; int count = lists.Count(); if (count = 0)//Judge the number of data pieces. If it is empty, the parameter will be assigned automatically { List<JinHuoVo> listJX = lists.Skip(bsgridPage.GetStartIndex()).Take(bsgridPage.pageSize).ToList(); Bsgrid<JinHuoVo> bsgrid = new Bsgrid<JinHuoVo>() { success = true, curPage = bsgridPage.curPage, totalRows = count, data = listJX }; return Json(bsgrid, JsonRequestBehavior.AllowGet); } else { var listKC = (from tbXSMX in myModels.Base_XiaoShoMX join tbXS in myModels.Base_XiaoShouDan on tbXSMX.XiaoShouDanID equals tbXS.XiaoShouDanID join tbKuCun in myModels.JC_KuCun on tbXSMX.KuCunID equals tbKuCun.KuCunID from tbJinHuoMX in myModels.Base_JinHuoMX where tbJinHuoMX.KuCunID == tbXSMX.KuCunID select new JinHuoVo { XiaoShoMXID = tbXSMX.XiaoShoMXID, XiaoJi = tbXSMX.XiaoJi, KuCunNumber = tbKuCun.KuCunNumber, //Inventory retention ShuLiang = 0, //The purchase quantity and sales quantity are assigned to 0; XiaoShouNumber = 0 }).ToList(); var listss = (from tb in listKC group tb by tb.XiaoJi into JX select new JinHuoVo { XiaoJi = JX.OrderByDescending(m => m.XiaoShoMXID).FirstOrDefault().XiaoJi, KuCunNumber = JX.Sum(m => m.KuCunNumber), ShuLiang = JX.Sum(m => m.ShuLiang), XiaoShouNumber = JX.Sum(m => m.XiaoShouNumber) }).ToList(); int KCcount = listss.Count(); List<JinHuoVo> listJXs = listss.Skip(bsgridPage.GetStartIndex()).Take(bsgridPage.pageSize).ToList(); Bsgrid<JinHuoVo> bsgrid = new Bsgrid<JinHuoVo>() { success = true, curPage = bsgridPage.curPage, totalRows = KCcount, data = listJXs }; return Json(bsgrid, JsonRequestBehavior.AllowGet); } }//Subtotal