我很好奇在使用实体框架时应该在调用链中的哪个位置调用。考虑以下方法:
// sample usage from service layer
// _customerRepository.Paginate(1, out totalRecords, 25, "DateJoined DESC, AmountSpent", "DateJoined >= '2009-02-01'", "Invoices, Refunds");
public virtual IQueryable<T> Paginate(int page, out int total, int pageSize, string sort = "Id", string filter = null, string includes = null)
{
IQueryable<T> query = DatabaseSet;
total = query.Count(); // get total # of records (required for pagination)...
var skipTo = GetValidSkipCount(page, total, pageSize);
if (!String.IsNullOrWhiteSpace(filter))
{
query.Where(filter);
}
// should includes be before filtering?
// should query.Count() be called after query.Include?
// does it matter?
if (!String.IsNullOrWhiteSpace(includes))
{
query.IncludeMany(includes); // my own extension that takes comma separated string of entities to include
}
return query.OrderBy(sort).Skip(skipTo).Take(pageSize);
}
我的问题是:
- Include 应该始终位于调用链的首位吗?
- Count() 会受到 Include 的影响吗?如果是这样,我想我应该在包含后进行计数
- 应该包括在过滤之前还是之后(在哪里)?
- 这真的很重要,因为 EF 足够“聪明”,可以弄清楚所有事情吗?