我有SQL查询,我想用linq重写它。这个sql命令只加入两个表并对它们进行分组。问题在于分组。当我在一个表上使用group by时,一切正常,linq commant返回与sql命令相同的结果。但是当我想加入两个表然后由H.Ucet分组(例如)然后它返回其他结果作为我的sql命令。结果与使用左连接时相同,但我想要默认的内连接。
这是sql命令:
string dotazBankUcty = @"SELECT
H.UCET,
SUM(H.MD) AS MD,
SUM(H.DAL) AS DAL ,
SUM(H.MD_M) AS MD_M,
SUM(H.DAL_ENA) DAL_M,
MAX(UBUC.KOD) AS KOD
FROM ACCOUNT H
inner join UBU on H.UCET = UBU.UCET GROUP BY H.UCET";
我尝试用这个linq命令重写它。
var accountQuery = new XPQuery<ACCOUNT >(CoreHelper.DataSession);
var ubuQuery = new XPQuery<UBU>(CoreHelper.DataSession);
var resultBankyUcty = (from h in accountQuery
join u in ubuQuery on h.CompoundKey1.UCET equals u.UCET
group new { h, u } by new { h.CompoundKey1.UCET } into gUcty
select new
{
Ucet = gUcty.Key.UCET,
MD = gUcty.Sum(k => k.h.MD),
DAL = gUcty.Sum(k => k.h.DAL),
MD_M = gUcty.Sum(k => k.h.MDM),
DAL_M = gUcty.Sum(k => k.h.DALM),
KOD = gUcty.Max(k => k.u.KOD)
});
任何人都可以帮助我吗?我看不出错误。
答案 0 :(得分:9)
from t in (
from h in accountquery
join u in ubuQuery on h.CompoundKey1.UCET equals u.UCET
select new {h,u}
) group t by new {t.h.CompoundKey1.UCET} into g
select new {
Ucet = g.Key.UCET,
MD = g.Sum(k => k.h.MD),
DAL = g.Sum(k => k.h.DAL),
MD_M = g.Sum(k => k.h.MDM),
DAL_M = g.Sum(k => k.h.DALM),
KOD = g.Max(k => k.u.KOD)
};