Hello Team,
Please I want to sum up product quantity and amount by same date. Thank you
public ActionResult GetAllPrintSalesRecord(string sDate = null, string eDate = null, string OrderNo = null)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var dataList = db.tblSales.Join(
db.tblSalesDetails,
sales => sales.SalesId,
detail => detail.SalesId,
(sales, detail) => new { Sales = sales, detail = detail });
var modifiedData = dataList.GroupBy(a => new { a.detail.ProductId, a.Sales.OrderDate }).Select(x => new
{
SalesDetailId = x.FirstOrDefault().detail.SalesDetailId,
Quantity = x.Sum(c => c.detail.Quantity),
ProductId = x.Key.ProductId,
ProductName = x.FirstOrDefault().detail.tblProduct.ProductName,
SalesId = x.FirstOrDefault().Sales.SalesId,
OrderDate = x.FirstOrDefault().Sales.OrderDate,
SubTotal = x.FirstOrDefault().Sales.SubTotal,
DiscountPercent = x.FirstOrDefault().Sales.DiscountPercent,
VatPercent = x.FirstOrDefault().Sales.VatPercent,
TotalAmount = x.Sum(c => c.Sales.TotalAmount)
}).ToList();
return Json(new { modifiedData = modifiedData, total = total, discount = discount, tax = tax }, JsonRequestBehavior.AllowGet);
}

Vishal JoshiRank #377 · 6.7K reputationPosted Oct 24, 2023, 4:02 PM EDT
Hello Emmanuel,
You can use Group by to sum the quantity and total amount of sales records. you can add key columns as orderDate and ProductId into the group by.
Please check the code below to get it to work.
var modifiedData = dataList.GroupBy(a => new { a.detail.ProductId, a.Sales.OrderDate }).Select(x => new { SalesDetailId = x.FirstOrDefault().detail.SalesDetailId, Quantity = x.Sum(c => c.detail.Quantity), ProductId = x.Key.ProductId, ProductName = x.FirstOrDefault().detail.tblProduct.ProductName, SalesId = x.FirstOrDefault().Sales.SalesId, OrderDate = x.FirstOrDefault().Sales.OrderDate, SubTotal = x.FirstOrDefault().Sales.SubTotal, DiscountPercent = x.FirstOrDefault().Sales.DiscountPercent, VatPercent = x.FirstOrDefault().Sales.VatPercent, TotalAmount = x.Sum(c => c.Sales.TotalAmount) }).ToList();Thanks
Vishal Joshi
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Oct 24, 2023, 6:10 PM EDT
Thank you Mr Vishal, it is working as per your correction