gpt4 book ai didi

c# - Linq group by 多表和内连接

转载 作者:太空狗 更新时间:2023-10-30 00:25:04 24 4
gpt4 key购买 nike

我有 SQL 查询,我想用 linq 重写它。此 sql 命令仅连接两个表并对它们进行分组。问题出在分组依据上。当我在一张表上使用 group by 时,一切正常,linq 命令返回与 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)
});

谁能帮帮我?我看不到错误。

最佳答案

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)
};

关于c# - Linq group by 多表和内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com