Linq get max()分组 [英] Linq get max() Grouping

查看:43
本文介绍了Linq get max()分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个SQL查询:

declare @p_Date datetime 
set @p_Date = '01/01/2015'

select * 
from inv_hist
where Secuencia in (
    select max(Secuencia)
        from inv_hist
        where DatFechaDocumento <= @p_Date
            group by Bodega,NumIdConcepto
    )

如何在LINQ中编写此查询?

How can I write this query in LINQ?

我整天都在忙.我不是linq方面的专家,其他类似问题也无法解决我的问题.

I was figthing with this all day. I'm not an expert on linq and other similar questions doesn't solve my problem.

推荐答案

尝试一下:

var query = from x in invHistory
                    join y in
                            (
                                from z in invHistory
                                where z.PDate == "01/01/2015"
                                group z by new { z.Bodega, z.NumIDConcepto } into g
                                select new
                                       {
                                           NewSecuencia = g.Max(s => s.Secuencia) 
                                       }
                            ) 
                            on x.Secuencia equals y.NewSecuencia
                    select x;

如果您想要隐式样式,请按照以下步骤操作:

If you want implicit style this is how you do it:

  var query2 = from x in invHistory
               from y in
                         (
                             from z in invHistory
                             where z.PDate == "01/01/2015"
                             group z by new { z.Bodega, z.NumIDConcepto } into g
                             select new
                             {
                                 NewSecuencia = g.Max(s => s.Secuencia)
                             }
                         )
               where 
                    x.Secuencia == y.NewSecuencia
               select x;

请注意,隐式样式联接语法(ANSI-89)不是标准的,并且已从SQL Server 2005中弃用.

Please note that the implicit style join syntax (ANSI-89) is not a standard and is deprecated from SQL Server 2005.

这篇关于Linq get max()分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆