Doubt syntax: How to find the minimum and maximum value of a field in Line with Where

Asked

Viewed 49 times

1

I need to create a function in linq return me the minimum and maximum values of a field in a list. I was able to perform an SQL query in this way, but I need this in the syntax in linq.

SELECT
MIN(DATALANCAMENTO),
MAX(DATALANCAMENTO)
FROM CONTACORRENTECOMISSOES
WHERE SEQUENCIALOTELOJISTA = 14 //

I tried to accomplish this way, but the system does not allow to use the .Where.

var dtMin = unit.ContaCorrenteComissoes.Min(c => c.DATALANCAMENTO).Where(c => c.SequenciaLoteLojista == nuLote
var dtMax = unit.ContaCorrenteComissoes.Max(c => c.DATALANCAMENTO).Where(c => c.SequenciaLoteLojista == nuLote)

Can help?

  • 1

    min does not return a list, so you cannot call Where later, in other words do Where first, call min last

1 answer

1

First you should use Where() then Min() or Max(), as both (min, max) return a unique value and Where only applies to Lists:

var dtMin = unit.ContaCorrenteComissoes.Where(c => c.SequenciaLoteLojista == nuLote).Min(c => c.DATALANCAMENTO);
var dtMax = unit.ContaCorrenteComissoes.Where(c => c.SequenciaLoteLojista == nuLote).Max(c => c.DATALANCAMENTO);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.