Skip to content

Instantly share code, notes, and snippets.

@FabianoCampos
Created September 21, 2018 16:05
Show Gist options
  • Save FabianoCampos/1c23093884f49fa34c4cb789e5cb56ba to your computer and use it in GitHub Desktop.
Save FabianoCampos/1c23093884f49fa34c4cb789e5cb56ba to your computer and use it in GitHub Desktop.
Row_number over (Partition by yyy) em Entity Framework
_contexto.Tramitacao
.Where(x => entrada.Contains(x.IdDocumento))
.GroupBy(x => new { x.IdDocumento })
.Select(g => g.OrderBy(e=> e.IdDocumento).ThenByDescending(e => e.Data).Take(1))
.SelectMany(e => e.Select(x => new TramitacaoDocumentoModel
{
IdDocumento = x.IdDocumento,
IdStatus = x.IdStatus,
DataTramitacao = x.Data
}));
SELECT IdDocumento ,
IdStatus ,
Data
FROM ( SELECT ROW_NUMBER() OVER ( PARTITION BY IdDocumento ORDER BY IdDocumento, Data DESC ) AS RowNum ,
IdDocumento ,
IdStatus ,
Data AS Data
FROM Tramitacao
WHERE IdDocumento IN (
'F69485DF-789F-4A80-9693-0B82FF94D5A6',
'D41418A7-C7ED-4D89-B2B1-39B81E99B1B0',
'48C7D794-5C8A-4800-B197-3B8D2BB9BC44',
'D642CF45-95CE-44C5-9710-A863B7CDBE02',
'DBA55031-BB3E-4684-B8DC-B869A92220A1',
'F88B5DEB-D6E7-42E9-B456-CF3DA0C3D869',
'7699A253-2628-4619-B163-D5B8707696CF' )
GROUP BY IdDocumento,IdStatus, Data
) C
WHERE RowNum = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment