Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用GroupBy并基于条件显示不同的和-LINQ Lambda表达式_Linq_Lambda_Group By_Sum - Fatal编程技术网

使用GroupBy并基于条件显示不同的和-LINQ 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)

我有一个有三列的表

我想展示一个具有以下结构的表格:

有人能告诉我如何使用Lambda表达式吗

到目前为止,如果我只想显示一列,我只能得到结果:

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)
            });
完成了