简体   繁体   English

使用Sum进行多个比较的C#lambda表达式

[英]c# lambda expression using Sum for multiple comparisons

I'm trying use the Sum method in a lambda expression for a comparison, but I want to use it for multiple comparisons. 我正在尝试在lambda表达式中使用Sum方法进行比较,但我想将其用于多个比较。 How do I accomplish this? 我该如何完成? I've looked at "Let" and "SelectMany", but I haven't been able to find an answer. 我看过“ Let”和“ SelectMany”,但找不到答案。

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. 我还想确保Sum大于某个最小值。 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 我只需要minValue <= .Sum()<= maxValue

It sounds like you want something like: 听起来您想要这样的东西:

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);

So the Select here is the equivalent of using a let in a query expression. 因此,此处的“ Select ”等效于在查询表达式中使用let

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;

To get the power of let , you need to switch from method-chaining syntax to query expression syntax. 要获得let ,您需要从方法链接语法转换为查询表达式语法。 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. 我注意到,这是检查只有好腿的宽度之和-我不相信这是你想要的,但它是你现在在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM