4

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

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

4

1 回答 1

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)  
                };
于 2013-10-16T09:50:32.490 回答