我还在学习linq,现在我很困惑。我需要来自 3 个表的报告数据,我得到了结果,但它是重复的。我使用了 distinct 但它不起作用,n 我需要数据总和仍然不起作用。
这是我的桌子
TBL_MKN_MNM TBL_DETAIL TBL_TRANSACTION
ID_A Name Price Stock ID_B ID_A ID_C MANY ID_C DATE PAY
11 pepsi 2500 15 1 11 1234 1 1234 2013-05-22 6000
22 coca-cola 3000 16 2 22 1234 1 6666 2013-05-22 10000
3 11 6666 2
4 22 6666 1
the result I want
Name MANY PRICE AMOUNT LeftStock
Pepsi 3 2500 7500 12
Coca-Cola 2 3000 6000 14
Total 13500
这是我的查询,有人可以解释什么是错的吗?
var report= (from u in myDb.TBL_TRANSAKSI_MKN_MNMs.AsEnumerable()
where u.TGL_TRANSAKSI.Value.Date.Equals(dateTimePicker1.Value.Date)
join l in myDb.TBL_DETAIL_TRANSAKSIs.AsEnumerable() on u.ID_NOTA equals l.ID_NOTA
join m in myDb.TBL_MKN_MNMs.AsEnumerable() on l.ID_MKN_MNM equals m.ID_MKN_MNM
group new { u, l, m } by new { m.NAMA_MKN_MNM, m.HARGA_JUAL, u.TGL_TRANSAKSI, l.ID_MKN_MNM, u.USERNAME, l.Jumlah }
into grp
select new
{
MakanMinum = grp.Key.NAMA_MKN_MNM,
HargaJual = grp.Key.HARGA_JUAL,
Stok = grp.Sum(groupedthing => groupedthing.l.Jumlah),
Tanggal = grp.Key.TGL_TRANSAKSI,
Jumlah =(grp.Key.HARGA_JUAL * grp.Sum(groupedthing => groupedthing.l.Jumlah)),
Total = grp.Sum(grouptotal => grp.Key.HARGA_JUAL * grp.Sum(groupedthing => groupedthing.l.Jumlah)),
Username = grp.Key.USERNAME
}).Distinct();