gpt4 book ai didi

c# - 实体查询中的 If Else 语句

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

我试图在实体查询中生成一个 if else 语句,我有以下代码:

var asset = dbContext.Set<Asset>()
.Include(x => x.Item)
.Include(x => x.Item.ItemDetailSubCategory)
.Include(x => x.AssetCurrentStatus)
.Include(x => x.AssetCurrentStatus.AssetCondition)
.Include(x => x.Currency)
.Include(x => x.AssetCurrentStatus.AssetAccessories)
.Where(x => x.AssetCurrentStatus.Room.Floor.IsActive == true)
.Where(x => x.AssetCurrentStatus.Room.Floor.Building.IsActive == true)
.Where(x => x.AssetCurrentStatus.Room.IsActive == true)
.ToList();

现在我希望是这样的:

var asset = dbContext.Set<Asset>()
.Include(x => x.Item)
.Include(x => x.Item.ItemDetailSubCategory)
.Include(x => x.AssetCurrentStatus)
.Include(x => x.AssetCurrentStatus.AssetCondition)
.Include(x => x.Currency)
.Include(x => x.AssetCurrentStatus.AssetAccessories)

if(x.AssetCurrentStatus.AssetLocation != null)
{
.Where(x => x.AssetCurrentStatus.Room.Floor.IsActive == true)
.Where(x => x.AssetCurrentStatus.Room.Floor.Building.IsActive == true)
.Where(x => x.AssetCurrentStatus.Room.IsActive == true)
}
else
{
.ToList()
}

有什么办法可以实现吗?提前致谢

最佳答案

好吧,if 没有意义,因为您希望在每个 Assets 上不仅应用该逻辑一次。所以你需要将它应用到查询本身:

var asset = dbContext.Set<Asset>()
.Include(....)
.Where(x => x.AssetCurrentStatus.AssetLocation == null
|| (x.AssetCurrentStatus.Room.Floor.IsActive
&& x.AssetCurrentStatus.Room.Floor.Building.IsActive
&& x.AssetCurrentStatus.Room.IsActive))
.ToList();

连续的&&与连续的Where相同

关于c# - 实体查询中的 If Else 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57689143/

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