在LINQ中使用Sub查询对一些字段进行分组和求和

时间:2016-01-07 03:45:12

标签: c# mysql linq

我试图将我的sql查询转换为linq,我对sum和分组感到困惑, 这是我的查询

SELECT 
  produk.supplier,
  SUM(transaksi.jumlah_transaksi),
  SUM(transaksi.nominal_transaksi),
  operasional.nominal 
FROM
  transaksi INNER JOIN produk ON transaksi.id_produk = produk.id_produk 
  LEFT JOIN 
    (SELECT 
      operasional.id_supplier,
      SUM(nominal) AS nominal 
    FROM
      operasional) operasional 
    ON operasional.id_supplier = produk.id_supplier 
GROUP BY produk.supplier 

输出应该是 像这样

enter image description here

进度 我只是尝试使用这样的linq查询而不进行分组

var result = from t in db.transaksi
             join p in db.produk on t.id_produk equals p.id_produk
             from op in 
             (
                  from o in db.operasional
                  select new
                  {
                       id_supplier = o.id_supplier,
                       nominal = o.nominal
                  }
             ).Where(o => o.id_supplier == p.id_supplier).DefaultIfEmpty()
             select new
             {
                  nama_supplier = p.supplier,
                  jumlah_transaksi = t.jumlah_transaksi,
                  nominal_transaksi = t.nominal_transaksi,
                  biaya_operasional = op.nominal
             };

来自我的linq的结果查询仍然像这样

SELECT 
  `p`.`supplier`,
  `t`.`jumlah_transaksi`,
  `t`.`nominal_transaksi`,
  `t1`.`nominal` 
FROM
  `transaksi` `t` 
  INNER JOIN `produk` `p` 
    ON `t`.`id_produk` = `p`.`id_produk` 
  LEFT JOIN `operasional` `t1` 
    ON `t1`.`id_supplier` = `p`.`id_supplier`

解决 这是我的完整linq

var result = from t in db.transaksi
             join p in db.produk on t.id_produk equals p.id_produk
             from op in 
             (
                  from o in db.operasional
                  group o by o.id_supplier into g
                  select new
                  {
                       id_supplier = g.First().id_supplier,
                       nominal = g.Sum(o => o.nominal)
                  }
             ).Where(o => o.id_supplier == p.id_supplier).DefaultIfEmpty()
             select new
             {
                  nama_supplier = p.supplier,
                  jumlah_transaksi = t.jumlah_transaksi,
                  nominal_transaksi = t.nominal_transaksi,
                  biaya_operasional = op.nominal
             };
var grouped = result
              .GroupBy(x => x.nama_supplier)
              .Select(x => new
              {
                   nama_supplier = x.Key,
                   jumlah_transaksi = x.Sum(s => s.jumlah_transaksi),
                   nominal_transaksi = x.Sum(s => s.nominal_transaksi),
                   biaya_operasional = x.Select(s => s.biaya_operasional).First()
              });

1 个答案:

答案 0 :(得分:0)

尝试使用GroupBy(在下面的代码中,结果是来自上面代码的查询):

var grouped = result
    .GroupBy(x => x.nama_supplier)
    .Select(x => new {
        nama_supplier = x.Key,
        sum1 = x.Sum(s => s.jumlah_transaksi),
        sum1 = x.Sum(s => s.nominal_transaksi),
        nominal = x.Select(s => s.biaya_operasional).First()
    })

未检查代码,因此请将其用作想法。