I’m trying use the Sum method in a lambda expression for a comparison, but I want to use it for multiple comparisons. How do I accomplish this? I’ve looked at “Let” and “SelectMany”, but I haven’t been able to find an answer.
Below is what the code looks like:
return _dbContext.All<Table>()
.Where(table => table.CurrentLevel <= salesCriteria.MaxTableLevel)
.Where(table => table.Leg
.Where(leg=> salesCriteria.StartDate <= leg.AddDate)
.Where(leg=> leg.AddDate <= salesCriteria.EndDate)
.Sum(leg => leg.Width) <= salesCriteria.MaxGoalAmount);
As you can see, I’m trying to get all Tables with certain criteria that have Legs with certain criteria and whose width all add up to be less than a certain value. I would also like to make sure that the Sum is greater than a certain min value. However, I can’t do that here since as soon as I do .Sum, I lose the list. So how would I accomplish that here? All I want is minValue <= .Sum() <= maxValue
It sounds like you want something like:
So the
Selecthere is the equivalent of using aletin a query expression.As a query expression, this would be: