gpt4 book ai didi

c# - 将.Where(..)附加到Linq.IQueryable

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

我正在构建Linq IQueryable,并希望根据特定条件添加不同的.Where()参数。在运行时,似乎未考虑附加的.Where()。可能做错了什么?

var query = Context.Sessions
.Where(s => s.UserID.Equals(currentUserID))
.Where(s => s.ClosedTime.HasValue)
.Where(s => !s.ApprovedTime.HasValue);

if (type == Models.EnvironmentType.A)
{
query.Where(s => s.BedroomID.HasValue);
}
else if (type == Models.EnvironmentType.B)
{
query.Where(s => s.HomeID.HasValue);
}


谢谢!

最佳答案

您需要将返回结果分配回查询。Where创建一个新查询,它不会更改原始查询。

query = query.Where(s => s.BedroomID.HasValue);

关于c# - 将.Where(..)附加到Linq.IQueryable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22310152/

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