Expresión c # lambda usando Sum para múltiples comparaciones

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

preguntado el 09 de marzo de 12 a las 16:03

Do you want to apply the sum to the set of legs that meet your previously-defined criteria, or to the overall set of legs before you apply your other criteria? -

2 Respuestas

Parece que quieres algo como:

return _dbContext.All<Table>()
        .Where(table => table.CurrentLevel <= salesCriteria.MaxTableLevel)
        .Select(table => new { 
           table, 
           legWidth = table.Leg
                           .Where(leg=> salesCriteria.StartDate <= leg.AddDate)
                           .Where(leg=> leg.AddDate <= salesCriteria.EndDate)
                           .Sum(leg => leg.Width)
        })
        .Where(x => x.legWidth <= salesCriteria.MaxGoalAmount &&
                    x.legWidth >= salesCriteria.MinGoalAmount)
        .Select(x => x.table);

Entonces el Select here is the equivalent of using a let in a query expression.

As a query expression, this would be:

return from table in _dbContext.All<Table>()
       where table.CurrentLevel <= salesCriteria.MaxTableLevel
       let legWidth = table.Leg
                           .Where(leg=> salesCriteria.StartDate <= leg.AddDate)
                           .Where(leg=> leg.AddDate <= salesCriteria.EndDate)
                           .Sum(leg => leg.Width)
       where legWidth <= salesCriteria.MaxGoalAmount &&
             legWidth >= salesCriteria.MinGoalAmount
       select table;

respondido 09 mar '12, 17:03

To get the power of let, you need to switch from method-chaining syntax to query expression syntax. Try this:

var goodTables = 
    from table in _dbContext.All<Table>()
    where table.CurrentLevel <= salesCriteria.MaxTableLevel
    let sumOfWidthOfGoodLegs = 
        table.Leg
        .Where(leg=> salesCriteria.StartDate <= leg.AddDate)
        .Where(leg=> leg.AddDate <= salesCriteria.EndDate)
        .Sum(leg => leg.Width)    
    where sumOfWidthOfGoodLegs <= salesCriteria.MaxGoalAmount
    // can insert another where on sumOfWidthOfGoodLegs here as required
    select table;

return goodTables.ToList();

I note that this is checking the width-sum of only the good legs - I'm not convinced this is what you want, but it's what you're doing at present.

respondido 09 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.