使用 Concat 合并多个 IQueryable 查询 (Entity Framework)
Merging multiple IQueryable queries using Concat (Entity Framework)
我还是不太清楚,我有两个问题,例如:
IQueryable<Ad> list1 = db.Ads.Where(x => x.IsModerated == true);
IQueryable<Ad> list2 = db.Ads.Where(x => x.TypeId == 1);
IQueryable<Ad> list = list1.Concat(list2);
foreach(var item in list)
{
...
}
是一两次数据库调用吗?
它只执行一次。这是延迟执行的示例,阅读此 blog 了解更多详细信息
IQueryable<Ad> list1 = db.Ads.Where(x => x.IsModerated == true); -- builds query 1, no db execution
IQueryable<Ad> list2 = db.Ads.Where(x => x.TypeId == 1); -- builds query 2, no db execution
IQueryable<Ad> list = list1.Concat(list2); -- -- builds combine query 'UNION ALL', no db execution
foreach(var item in list) -- query executes here
{
...
}
我还是不太清楚,我有两个问题,例如:
IQueryable<Ad> list1 = db.Ads.Where(x => x.IsModerated == true);
IQueryable<Ad> list2 = db.Ads.Where(x => x.TypeId == 1);
IQueryable<Ad> list = list1.Concat(list2);
foreach(var item in list)
{
...
}
是一两次数据库调用吗?
它只执行一次。这是延迟执行的示例,阅读此 blog 了解更多详细信息
IQueryable<Ad> list1 = db.Ads.Where(x => x.IsModerated == true); -- builds query 1, no db execution
IQueryable<Ad> list2 = db.Ads.Where(x => x.TypeId == 1); -- builds query 2, no db execution
IQueryable<Ad> list = list1.Concat(list2); -- -- builds combine query 'UNION ALL', no db execution
foreach(var item in list) -- query executes here
{
...
}