计算总数

  1. 拿一个进货单当例子讲解,比如我选择有一个数据库里面有进货单表、进货单明细表、商品表,每一张进货单都对应有多个商品,每个商品进货数量和单价都不一样,因为总计金额并没有存在数据库里而是需要自己去计算,那么每个商品的单价和数量我们是知道的,那么我们想要计算总金额就必须知道每个商品的总金额,把每个商品的总金额的相加就可以得到进货单的总金额,我们需要先查询出进货单数据,在类purchaseOrderData里添加一个字段TotalQuantityReceived用于接收进货单的总金额,

#region 查询数据
1、获取到所有的进货单ID
List< purchaseOrderData > myStockIndentData = (from tbStockInvoices in myModel.PW_StockInvoices//进货单表

                                                     select new purchaseOrderData{StockInvoicesID = tbStockInvoices.StockInvoicesID,//进货单ID}).ToList();

2、获取到所有进货单详情表里面的每个商品的进货数量和单价

        List<ABC> stCommodity = (from tbsStockInvoicesDetail in myModel.PW_StockInvoicesDetail//进货订单明细表join tbCommodity in myModel.PW_Commodity on tbsStockInvoicesDetail.CommodityID equals tbCommodity.CommodityID //商品表select new ABC{

//进货单ID
StockInvoicesID = tbsStockInvoicesDetail.StockInvoicesID,
//商品名称
CommodityName = tbCommodity.CommodityName,
//进货数量
StockQuantity = tbsStockInvoicesDetail.StockQuantity,
//单价
Univalence = tbsStockInvoicesDetail.Univalence.ToString(),

                                 }).ToList();//通过foe循环获取每个商品的金额和单价,然后通过 “商品数量*单价”得到每个商品的总金额,在类	ABC 里声明字段SingleAmount保存每个商品的总金额for (var i = 0; i < stCommodity.Count; i++){ABC D = stCommodity[i];string A = D.StockQuantity;string B = D.Univalence;decimal C = Convert.ToDecimal(A) * Convert.ToDecimal(B);D.SingleAmount = C;//将总金额赋值给 stCommodity[i].SingleAmount}//循环遍历通过进货单ID来筛选出每一个进货单的多个商品金额 ,

获取每个进货单的所有商品的金额相加得到总金额

        for (var i = 0; i < myStockIndentData.Count; i++){purchaseOrderData a = myStockIndentData[i];int id = a.StockInvoicesID;//进货单IDfor (var j = 0; j < stCommodity.Count; j++){ABC b = stCommodity[j];if (b.StockInvoicesID == id)

//通过判断两个进货订单ID是否相同来筛选出每个进货单的商品金额

                {if (a.CommodityName == null){a.TotalQuantityReceived = Convert.ToDecimal(b.StockQuantity);}else{a. TotalQuantityReceived += +Convert.ToDecimal(b.StockQuantity);//将每个金额相加}}}}#endregion

获取到的两个数据都有进货单ID,想要获取到进货单的所有进货单详情ID也就获取到了所有的商品ID,但是在进货订单详情表和商品表连表查询就已经得到商品的数据

#region 分页
var intTotalRow = myStockIndentData.Count();
List list = myStockIndentData
.Skip(layuiTablePage.GetStartIndex())
.Take(layuiTablePage.limit)
.ToList();
//调用分页封装类
LayuiTableData layuiTableData = new LayuiTableData();
layuiTableData.count = intTotalRow;//行总数
layuiTableData.data = list;//具体显示数据
#endregion

计算总数

  1. 拿一个进货单当例子讲解,比如我选择有一个数据库里面有进货单表、进货单明细表、商品表,每一张进货单都对应有多个商品,每个商品进货数量和单价都不一样,因为总计金额并没有存在数据库里而是需要自己去计算,那么每个商品的单价和数量我们是知道的,那么我们想要计算总金额就必须知道每个商品的总金额,把每个商品的总金额的相加就可以得到进货单的总金额,我们需要先查询出进货单数据,在类purchaseOrderData里添加一个字段TotalQuantityReceived用于接收进货单的总金额,

#region 查询数据
1、获取到所有的进货单ID
List< purchaseOrderData > myStockIndentData = (from tbStockInvoices in myModel.PW_StockInvoices//进货单表

                                                     select new purchaseOrderData{StockInvoicesID = tbStockInvoices.StockInvoicesID,//进货单ID}).ToList();

2、获取到所有进货单详情表里面的每个商品的进货数量和单价

        List<ABC> stCommodity = (from tbsStockInvoicesDetail in myModel.PW_StockInvoicesDetail//进货订单明细表join tbCommodity in myModel.PW_Commodity on tbsStockInvoicesDetail.CommodityID equals tbCommodity.CommodityID //商品表select new ABC{

//进货单ID
StockInvoicesID = tbsStockInvoicesDetail.StockInvoicesID,
//商品名称
CommodityName = tbCommodity.CommodityName,
//进货数量
StockQuantity = tbsStockInvoicesDetail.StockQuantity,
//单价
Univalence = tbsStockInvoicesDetail.Univalence.ToString(),

                                 }).ToList();//通过foe循环获取每个商品的金额和单价,然后通过 “商品数量*单价”得到每个商品的总金额,在类	ABC 里声明字段SingleAmount保存每个商品的总金额for (var i = 0; i < stCommodity.Count; i++){ABC D = stCommodity[i];string A = D.StockQuantity;string B = D.Univalence;decimal C = Convert.ToDecimal(A) * Convert.ToDecimal(B);D.SingleAmount = C;//将总金额赋值给 stCommodity[i].SingleAmount}//循环遍历通过进货单ID来筛选出每一个进货单的多个商品金额 ,

获取每个进货单的所有商品的金额相加得到总金额

        for (var i = 0; i < myStockIndentData.Count; i++){purchaseOrderData a = myStockIndentData[i];int id = a.StockInvoicesID;//进货单IDfor (var j = 0; j < stCommodity.Count; j++){ABC b = stCommodity[j];if (b.StockInvoicesID == id)

//通过判断两个进货订单ID是否相同来筛选出每个进货单的商品金额

                {if (a.CommodityName == null){a.TotalQuantityReceived = Convert.ToDecimal(b.StockQuantity);}else{a. TotalQuantityReceived += +Convert.ToDecimal(b.StockQuantity);//将每个金额相加}}}}#endregion

获取到的两个数据都有进货单ID,想要获取到进货单的所有进货单详情ID也就获取到了所有的商品ID,但是在进货订单详情表和商品表连表查询就已经得到商品的数据

#region 分页
var intTotalRow = myStockIndentData.Count();
List list = myStockIndentData
.Skip(layuiTablePage.GetStartIndex())
.Take(layuiTablePage.limit)
.ToList();
//调用分页封装类
LayuiTableData layuiTableData = new LayuiTableData();
layuiTableData.count = intTotalRow;//行总数
layuiTableData.data = list;//具体显示数据
#endregion