简体   繁体   English

使用 LINQ 对连接表中的列求和以获取计算值

[英]Sum Columns from joined tables to get a calculated value using LINQ

In the below context I have joined two table based on some criteria.在下面的上下文中,我根据一些标准加入了两个表。 Now I need to calculate total value from the result of that joined table .现在我需要根据joined table的结果calculate total value

[HttpGet("inner-join/{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetReport(DateTime id)
{
  try
  {
    IEnumerable<BTBPending> objBTBPendingList = _unitOfWork.BTBPending.GetAll(includeProperties: "ProformaInvoice,ContractList,SupplierList,CountryList,ItemList,BuyerList,StyleList,TradeTermList,ErpRemarksList,StatusList,LcNoList,UdAmendList");

    IEnumerable<ProformaInvoice> objProformaInvoiceList = _unitOfWork.ProformaInvoice.GetAll(includeProperties: "ActualContract,ContractList,SupplierList,CountryList,ItemList,BuyerList,StyleList,TradeTermList");


    var query = objBTBPendingList
        .Where(x => x.LcOpenDate.Value.Year == id.Year && x.LcOpenDate.Value.Month == id.Month)
        .Where(x => x.CountryListId == 26)
        .Where(x => x.StatusListId == 12 || x.StatusListId == 13 || x.StatusListId == 14)
        .Join(objProformaInvoiceList,
              btbPending => btbPending.ContractListId,
              pi => pi.ContractListId, 
              (btbPending, pi) => new
              {
                LcNo = btbPending.LcNoList,
                Value = btbPending.PiValue,

                ContractNo = pi.ContractList,
                Buyer = pi.BuyerList,
                PiNo = pi.PINo,
                Supplier = pi.SupplierList,
                Item = pi.ItemList
              }).ToList();


     return Ok(query);
   }
 catch (Exception ex)
   {
     return StatusCode(500, "Internal Server Error, Please Try Again Leter!");
   }
}

After joining the these two table I need to return the sum from the below Value = btbPending.PiValue Column which I got after joining the tables.加入这两个表后,我需要从下面的Value = btbPending.PiValue列返回sum ,这是我after joining表后得到的。

.Join(objProformaInvoiceList,
      btbPending => btbPending.ContractListId,
      pi => pi.ContractListId, 
      (btbPending, pi) => new
      {
      LcNo = btbPending.LcNoList,
      Value = btbPending.PiValue,

      ContractNo = pi.ContractList,
      Buyer = pi.BuyerList,
      PiNo = pi.PINo,
      Supplier = pi.SupplierList,
      Item = pi.ItemList
      }).ToList();

You can calculate the sum like this var sum = query.Select(c => c.Value).Sum();您可以像这样计算总和var sum = query.Select(c => c.Value).Sum();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM