Linq集团由内部加入

时间:2013-10-15 08:39:05

标签: c# sql linq

我想替换这个SQL命令:

string dotazBankUcty = @"SELECT 
     H.UCET, 
     SUM(H.KON_STAV_MD) AS KON_STAV_MD, 
     SUM(H.KON_STAV_DAL) AS KON_STAV_DAL , 
     SUM(H.KON_STAV_MD_MENA) AS KON_STAV_MD_MENA, 
     SUM(H.KON_STAV_DAL_MENA) AS KON_STAV_DAL_MENA, 
     MAX(UBUC.KOD_MENY) AS KOD_MENY 
     FROM UHLAVAKT H 
     inner join UBUC on H.UCET = UBUC.UCET GROUP BY H.UCET";

使用LINQ命令。我尝试了这个,但是它返回了一个左连接而不是内连接,这是我想要的。

var uhlavAktQuery = new XPQuery<UHLAVAKT>(CoreHelper.DataSession);

var ubucQuery = new XPQuery<UBUC>(CoreHelper.DataSession);

var resultBankyUcty = (from h in uhlavAktQuery
    join u in ubucQuery on h.CompoundKey1.UCET equals u.UCET
    group new { h, u } by new { h.CompoundKey1.UCET } into gUcty
                    select new
                    {
                    Ucet = gUcty.Key.UCET,
                    KON_STAV_MD = gUcty.Sum(k => k.h.KONSTAVMD),
                    KON_STAV_DAL = gUcty.Sum(k => k.h.KONSTAVDAL),
                    KON_STAV_MD_MENA = gUcty.Sum(k => k.h.KONSTAVMDMENA),
                    KON_STAV_DAL_MENA = gUcty.Sum(k => k.h.KONSTAVDALMENA),
                    KOD_MENY = gUcty.Max(k => k.u.KODMENY)
                    });

请帮帮我吗?

1 个答案:

答案 0 :(得分:1)

当我努力想要加入LINQ时,我发现以下关于CodeProject的文章非常有用且有帮助:

LINQ Extended Joins

借助包含图表的示例,他解释了连接的内容,并提供了实现各种连接查询的IEnumerable的通用扩展方法。