gpt4 book ai didi

c# - 在 C# 中使用 EF 查询中的多个 Where 子句

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

我有两个 IQueryable 查询:

IQueryable<Ad> list1 = db.Ads.Where(x=>x.isModerated == true);
IQueryable<Ad> list2;

if(something == true)
{
list2 = list1.Where(x=>x.TypeId == 2)
// is it going to be the same as query below?
IQueryable<Ad> list1 = db.Ads.Where(x=>x.isModerated == true && x.TypeId == 2)
}

如果我想使用 OR (||) 运算符获取数据怎么办是相同的情况。

而且我相信在我想要迭代此查询之前,这将是一次数据库调用。正确的?

最佳答案

is it going to be the same as query below?

and what if I would like to get a data using OR (||) operator is the same scenario.

这样做的资源成本要低得多:

IQueryable<Ad> list1 = db.Ads.Where(x=>x.isModerated);
IQueryable<Ad> list2;

if(something == true)
{
list2 = list1.Where(x=>x.TypeId == 2)
}
else
{
list2 = db.Ads.Where(x=>x.isModerated || x.TypeId == 2)
}

但是如果出于某种原因你不想那样做,你可以这样做:

list2 = list1.Union(db.Ads.Where(x=> x.TypeId == 2)) 

会给你相同的结果集

关于c# - 在 C# 中使用 EF 查询中的多个 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51410470/

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