使用GroupBy并基于条件显示不同的和-LINQ Lambda表达式
我有一个有三列的表 我想展示一个具有以下结构的表格: 有人能告诉我如何使用Lambda表达式吗 到目前为止,如果我只想显示一列,我只能得到结果:使用GroupBy并基于条件显示不同的和-LINQ Lambda表达式,linq,lambda,group-by,sum,Linq,Lambda,Group By,Sum,我有一个有三列的表 我想展示一个具有以下结构的表格: 有人能告诉我如何使用Lambda表达式吗 到目前为止,如果我只想显示一列,我只能得到结果: var sum_data = _context.HechosFinanza .Where(x => x.Product.Sale_Type == "Cash Sale") .GroupBy(x => x.Product.Product_Name)
var sum_data = _context.HechosFinanza
.Where(x => x.Product.Sale_Type == "Cash Sale")
.GroupBy(x => x.Product.Product_Name)
.Select(x => Product { Tienda = x.Key, Total = x.Sum(s =>
s.sales_amount) });
我不知道这样的事情是否可能真的不知道,只是想弄明白:
var sum_data = _context.HechosFinanza
// I remove there where condition from here
.GroupBy(x => x.Product.Product_Name)
// And I add the where condition in each sum
.Select(x => Product { Tienda = x.Key,
TotalCash = x.Sum(s => s.sales_amount).Where(s => s.Product.Sale_Type == "Cash Sale"),
TotalCredit = x.Sum(s => s.sales_amount).Where(s.Product.Sale_Type == "Credit Sale")
});
嗯,嗯。结果我真的很接近 只需要把“Where”语句放在前面 答复:
var sum_data = _context.HechosFinanza
// I remove there where condition from here
.GroupBy(x => x.Product.Product_Name)
// And I add the where condition in each sum
.Select(x => Product { Tienda = x.Key,
TotalCash = x.Where(s => s.Product.Sale_Type == "Cash Sale").Sum(s => s.sales_amount),
TotalCredit = x.Where(s.Product.Sale_Type == "Credit Sale") .Sum(s => s.sales_amount)
});
完成了