gpt4 book ai didi

c# - 我怎样才能让 linq 查询有条件地不应用 where 子句

转载 作者:行者123 更新时间:2023-11-30 13:46:19 25 4
gpt4 key购买 nike

我什至不知道如何表达我想要的,所以这里有一些代码!

//jobTerm
if (!String.IsNullOrEmpty(vm.SelectedJobTerm))
{
vacancies = vacancies.Where(x => (!x.IsExternalPost
&& x.JobTerm.Name.Contains(vm.SelectedJobTerm)));
}
//jobType
if (!String.IsNullOrEmpty(vm.SelectedJobType))
{
vacancies = vacancies.Where(x => (!x.IsExternalPost
&& x.JobType.Name.Contains(vm.SelectedJobType)));
}

上面发生的事情是我正在运行一系列 if 语句,并在某些情况适用时添加到我的 linq 查询中。我将执行推迟到最后。如果有帮助,这是用于搜索结果过滤器的。

在上面的代码中,您看到我试图在 Where 子句中放入我希望查询不关心 vm.SelectedJobTerm 或 vm.SelectedJobType 的空缺职位是外部职位。我需要这个来应用到我几乎所有的过滤器。逻辑应该是这样的

-Is there a value in vm.SomeValue?
|_____>Yes
| |_____>Is this an external post?
| |__>Yes->do not apply where clause
| |
| |__>No->Apply the where clause stuff
|
|____>No-> Go about your business.

有没有办法在 linq 中做到这一点?

我应该注意,我正在访问一个 azure 数据库,所以我无法通过分析器运行它。飞得有点盲目。

最佳答案

您目前正在排除 IsExternalPost == true 的记录。

这将包括他们:

vacancies = vacancies.Where(x =>
x.IsExternalPost || x.JobTerm.Name.Contains(vm.SelectedJobTerm));

关于c# - 我怎样才能让 linq 查询有条件地不应用 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com