简体   繁体   English

左边连接group by和sum in linq to sql

[英]left join with group by and sum in linq to sql

I am trying to convert T-sql which is 我正在尝试转换T-sql

select c.description, sum(t.quantity), sum(t.Price) from trn_itm t
left join CAFE c on t.Item_key = c.dfm_key
where transaction_key = 87378
group by c.Description

my linq is 我的linq是

var query = from p in DfmSession.CurrentContext.TRN_ITMs
            join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t
            from rt in t.DefaultIfEmpty()
            where p.Transaction_key == _dfmkey
            select new
            {
                Dfm_key = (int?)rt.Dfm_key,
                rt.Description,
                p.Quantity,
                p.Price
            };

How do I get Sum(p.Quantity) and Sum(p.Price)? 我如何得到Sum(p.Quantity)和Sum(p.Price)?

var query = from p in DfmSession.CurrentContext.TRN_ITMs
            join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t
            from rt in t.DefaultIfEmpty()
            where p.Transaction_key == _dfmkey
            group p by rt.Description into g
            select new
            {
                Description = g.Keu
                Quantity = g.Select(x => x.Quantity).Sum()
                Price = g.Select(x => x.Price).Sum()
            };
var query = from p in DfmSession.CurrentContext.TRN_ITMs
        join c in DfmSession.CurrentContext.CAFEs on p.Item_key equals c.Dfm_key into t
        from rt in t.DefaultIfEmpty()
        where p.Transaction_key == _dfmkey
        group by rt.Description into g
        select new
     {
         description = g.Key,
         Price = g.Sum(x => x.Price),
         Quantity = g.Sum(x => x.Quantity)
     };
group by rt.Description into g

然后在g上使用您选择的聚合函数,例如:

select new { Price = g.Sum(x => x.Price) }
group by su.Description into h

然后在h上使用您选择的聚合方法,例如:

select new { Price = h.Sum(y =? y.Price) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM