C# 查询中的多个条件检查?

C# 查询中的多个条件检查?,c#,entity-framework,linq,C#,Entity Framework,Linq,我在下面的查询中从UI传入了4个参数。BU和地区在UI中有单选按钮选项(全部、选项1、选项2、选项3等)。选项1-3是数据中的值,而“全部”不是 有没有办法更有效地写下面的内容?例如,如果BU或地区值是UI中的“全部”(不是数据集中的值),那么如果选择了“全部”,我是否可以为BU或地区分配一个字符,以便查询知道选择所有值 我不知道是否有这样的事情,但我想避免做下面这样的事情 public string GetAvgSeatPrice(string bu, string region,

我在下面的查询中从UI传入了4个参数。BU和地区在UI中有单选按钮选项(全部、选项1、选项2、选项3等)。选项1-3是数据中的值,而“全部”不是

有没有办法更有效地写下面的内容?例如,如果BU或地区值是UI中的“全部”(不是数据集中的值),那么如果选择了“全部”,我是否可以为BU或地区分配一个字符,以便查询知道选择所有值

我不知道是否有这样的事情,但我想避免做下面这样的事情

     public string GetAvgSeatPrice(string bu, string region, DateTime? startDate, DateTime? endDate)
    {

        if (bu.Equals("All"))
        {
            var averageSeatPrice = (from r in db.Registrations
                                    where
                                         //r.BusinessUnit.Equals(bu) &&
                                         r.Region.Equals(region) &&
                                         r.StartDate >= startDate &&
                                         r.EndDate <= endDate &&
                                         r.ActualPrice > 0
                                    select r.ActualPrice).Average();

            var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

            return AvgSeatPrice;
        }
        else if (region.Equals("All"))
        {
            var averageSeatPrice = (from r in db.Registrations
                                    where
                                         r.BusinessUnit.Equals(bu) &&
                                        // r.Region.Equals(region) &&
                                         r.StartDate >= startDate &&
                                         r.EndDate <= endDate &&
                                         r.ActualPrice > 0
                                    select r.ActualPrice).Average();

            var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

            return AvgSeatPrice;
        }
        else if (bu.Equals("All") && region.Equals("All"))
        {
            var averageSeatPrice = (from r in db.Registrations
                                    where
                                         //r.BusinessUnit.Equals(bu) &&
                                         // r.Region.Equals(region) &&
                                         r.StartDate >= startDate &&
                                         r.EndDate <= endDate &&
                                         r.ActualPrice > 0
                                    select r.ActualPrice).Average();

            var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

            return AvgSeatPrice;
        }
        else
        {
            var averageSeatPrice = (from r in db.Registrations
                                    where
                                         r.BusinessUnit.Equals(bu) &&
                                         r.Region.Equals(region) &&
                                         r.StartDate >= startDate &&
                                         r.EndDate <= endDate &&
                                         r.ActualPrice > 0
                                    select r.ActualPrice).Average();

            var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

            return AvgSeatPrice;
        }

    }

}
public string GetAvgSeatPrice(string bu、string region、DateTime?startDate、DateTime?endDate)
{
如果(bu.等于(“全部”))
{
var averageSeatPrice=(以db为单位从r开始)
哪里
//r、 业务单位。等于(bu)&&
r、 区域。等于(区域)&&
r、 开始日期>=开始日期&&
r、 结束日期0
选择r.ActualPrice).Average();
var AvgSeatPrice=“$”+string.Format(“{0:0.00}”,averageSeatPrice);
返回AvgSeatPrice;
}
else if(region.Equals(“All”))
{
var averageSeatPrice=(以db为单位从r开始)
哪里
r、 业务单位。等于(bu)&&
//r.区域等于(区域)&&
r、 开始日期>=开始日期&&
r、 结束日期0
选择r.ActualPrice).Average();
var AvgSeatPrice=“$”+string.Format(“{0:0.00}”,averageSeatPrice);
返回AvgSeatPrice;
}
如果(业务单位等于(“全部”)&地区等于(“全部”))
{
var averageSeatPrice=(以db为单位从r开始)
哪里
//r、 业务单位。等于(bu)&&
//r.区域等于(区域)&&
r、 开始日期>=开始日期&&
r、 结束日期0
选择r.ActualPrice).Average();
var AvgSeatPrice=“$”+string.Format(“{0:0.00}”,averageSeatPrice);
返回AvgSeatPrice;
}
其他的
{
var averageSeatPrice=(以db为单位从r开始)
哪里
r、 业务单位。等于(bu)&&
r、 区域。等于(区域)&&
r、 开始日期>=开始日期&&
r、 结束日期0
选择r.ActualPrice).Average();
var AvgSeatPrice=“$”+string.Format(“{0:0.00}”,averageSeatPrice);
返回AvgSeatPrice;
}
}
}

}我希望这并不是你所要求的没有提供的(我可以从你的问题中用两种不同的方式解释多个if/else案例/etc)。但你可以这样做:

var averageSeatPrice = 
    from r in db.Registrations
    where r.StartDate >= startDate &&
          r.EndDate <= endDate &&
          r.ActualPrice > 0

// note I'm assuming your "bu" "all" option is represented as the string "All"
if (bu.ToUpper() != "ALL") 
    averageSeatPrice = averageSeatPrice
        .Where(w => w.BusinessUnit == bu);

if (region.ToUpper() !+ "ALL")
    averageSeatPrice = averageSeatPrice
        .Where(w => w.Region == region);

averageSeatPrice = averageSeatPrice select r.ActualPrice).Average();
如果
bu
作为
option1
提供,那么我们需要where子句包含
where BusinessUnit='option1'

因此:

另一方面,如果
bu
是“ALL”或string.empty,或者您希望它是无效的值,那么我们根本不希望BusinessUnit成为where子句的一部分。。。发布的代码也完成了此场景:

if (bu.ToUpper() != "ALL") // this evaluates to false (bu is "ALL"), don't add the where clause information at all.
    averageSeatPrice = averageSeatPrice
        .Where(w => w.BusinessUnit == bu);
每个
averageSeatPrice=averageSeatPrice。其中(…)
仅附加到查询上,直到枚举结果为止。

只需执行以下操作:

public string GetAvgSeatPrice(string bu, string region, DateTime? startDate, DateTime? endDate)
    {
        var averageSeatPrice = (
            from r in db.Registrations
            where (bu == "ALL" || r.BusinessUnit.Equals(bu))
            && (region == "ALL" || r.Region.Equals(region))
            && r.StartDate >= startDate
            && r.EndDate <= endDate
            && r.ActualPrice > 0
            select r.ActualPrice).Average();

        var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

        return AvgSeatPrice;
    }
public string GetAvgSeatPrice(string bu、string region、DateTime?startDate、DateTime?endDate)
{
var平均座位价格=(
从r到db.注册
其中(bu==“所有”| r.BusinessUnit.Equals(bu))
&&(region==“ALL”| | r.region.Equals(region))
&&r.StartDate>=StartDate
&&r.EndDate 0
选择r.ActualPrice).Average();
var AvgSeatPrice=“$”+string.Format(“{0:0.00}”,averageSeatPrice);
返回AvgSeatPrice;
}

由于OR(| |)的原因,如果选择为“全部”,则不会选中第二个条件,也不会根据给定的属性缩小选择范围。只有当所选内容不是“全部”,并且在这种情况下所选内容缩小时,才会选中该选项。

当前代码有什么问题?您有错误吗?如果我传入选项1、2或3,它会工作,因为这些是数据中的值,我可以分配给这些选项。如果我选择“全部”,则没有可供比较的内容,因为在Where中没有可分配给所有可供比较的内容的值。我想知道是否有一个字符可以分配给All选项,这样它就不会过滤。谢谢,但是,“All”不是数据集中的值,我正在寻找一个字符来替换“All”,这样我就可以在查询之前将其分配给bu,这样查询就不会过滤。我甚至不知道是否有这样的事情,但我正在试图找到一个更好的解决方案,上面的代码我更新,试图澄清我的愿望。我看了你更新的问题,我仍然认为我的答案正是你想要的。请注意,我没有将“ALL”与第一部分中的数据集(在| |之前)进行比较。我只需检查来自UI的内容,如果是“全部”,则整个条件已为真,第二部分从不检查,这意味着您将获得所有记录。假设BU字段中的唯一值为“猫”、“狗”和“鸟”。如果从UI传入“ALL”,则基于
if (bu.ToUpper() != "ALL") // this evaluates to false (bu is "ALL"), don't add the where clause information at all.
    averageSeatPrice = averageSeatPrice
        .Where(w => w.BusinessUnit == bu);
public string GetAvgSeatPrice(string bu, string region, DateTime? startDate, DateTime? endDate)
    {
        var averageSeatPrice = (
            from r in db.Registrations
            where (bu == "ALL" || r.BusinessUnit.Equals(bu))
            && (region == "ALL" || r.Region.Equals(region))
            && r.StartDate >= startDate
            && r.EndDate <= endDate
            && r.ActualPrice > 0
            select r.ActualPrice).Average();

        var AvgSeatPrice = "$" + string.Format("{0:0.00}", averageSeatPrice);

        return AvgSeatPrice;
    }