gpt4 book ai didi

c# - 如何在 Linq 查询中的 where 条件下使用数组

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:01 25 4
gpt4 key购买 nike

这是我的 linq 查询。

var data =
(from obj in lstEventsDataForGrid
where obj.strDateCode.Contains(thisWeekend[0] == null ? "" : thisWeekend[0])
|| obj.strDateCode.Contains(thisWeekend[1] == null ? "$" : thisWeekend[1])
|| obj.strDateCode.Contains(thisWeekend[2] == null ? "$" : thisWeekend[2])
&& strArr.Contains(obj.strEventType.ToString().ToUpper())
orderby obj.dtmStartDate ascending
select obj).GroupBy(x => x.strEventCode)
.Select(y => y.First()).ToArray();

预期结果

strEventType不在strArr中应该不会来

但即使该类型不在数组中,它也会出现。

问题 我注意到如果我删除一个 where 条件,即 obj.strDateCode.Contains(...) 另一个条件有效。

我哪里错了?请提出建议!

最佳答案

我使用空合并运算符重写了您的查询,以使其更具可读性。我还添加了行号以指出我认为这里的错误:

1.     var data = (
2. from obj in lstEventsDataForGrid
3. where obj.strDateCode.Contains(thisWeekend[0] ?? "") ||
4. obj.strDateCode.Contains(thisWeekend[1] ?? "$") ||
5. obj.strDateCode.Contains(thisWeekend[2] ?? "$") &&
6. strArr.Contains(obj.strEventType.ToString().ToUpper())
7. orderby obj.dtmStartDate ascending
8. select obj
9. ).GroupBy(x => x.strEventCode).Select(y => y.First()).ToArray();

您需要更改以下行:

3.         where (obj.strDateCode ...          // add '('
5. ... thisWeekend[2] ?? "$")) && // add ')'

这样,您的 && 将压倒其余条件。

关于c# - 如何在 Linq 查询中的 where 条件下使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712358/

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