IQueryable
ToList 之后调用的 Where 语句是 Enumerable.Where(),这个是在程序内执行的,所以能调用 schoolIds 这个对象。
建议的写法是:
if (schoolIds == null) return GetSchoolAttributeValues(declarationId).ToList();
return GetSchoolAttributeValues(declarationId).ToList().Where(z => schoolIds.Contains(z));
可以避免在 schoolIds 为空时候缺调用永远为 true 的 Where 语句,产生额外的消耗;
更好的写法是,如果预期 schoolIds 不为 null 时,如果它的数量超过某个值(比如 10)条时,使用 var set = new HashSet(schoolIds); 创建一个 set 再来进行 Contains 比较。性能会更好。