我有一种方法,我试图返回所有具有匹配性别的默认客户地址。我希望能够通过将 System.Func 方法传递给 where 子句来一点一点地构建过滤查询。
var emailAddresses = new List<string>();
// get all customers.
IQueryable<Customer> customersQ = base.GetAllQueryable(appContext).Where(o => o.Deleted == false);
// for each customer filter, filter the query.
var genders = new List<string>() { "C" };
Func<Customer, bool> customerGender = (o => genders.Contains(o.Addresses.FirstOrDefault(a => a.IsDefaultAddress).Gender));
customersQ = customersQ.Where(customerGender).AsQueryable();
emailAddresses = (from c in customersQ
select c.Email).Distinct().ToList();
return emailAddresses;
但是这种方法对每个地址(8000)次调用数据库非常慢。
但是,如果我替换这两行
Func<Customer, bool> customerGender = (o => genders.Contains(o.Addresses.FirstOrDefault(a => a.IsDefaultAddress).Gender));
customersQ = customersQ.Where(customerGender).AsQueryable();
用一根线
customersQ = customersQ.Where(o => genders.Contains(o.Addresses.FirstOrDefault(a => a.IsDefaultAddress).Gender)).AsQueryable();
然后查询只对数据库进行一次调用,速度非常快。
我的问题是为什么这会有所作为?如何使第一种方法只调用一次数据库?