我有一個如下的控制器,它需要太長時間加載數據。我正在使用contains和tolist()方法。我聽說過toList()方法的低性能。什麼是toList()方法的性能替代。
我怎樣才能改變這種方法更好的性能編碼。
public List<decimal> GetOrgSolution()
{
//Need to use USER id. but we have EMPNO in session.
var Users = db.CRM_USERS.Where(c => c.ID == SessionCurrentUser.ID || RelOrgPerson.Contains(c.EMPNO.Value)).Select(c => c.ID);
//Get the organization list regarding to HR organization
var OrgList = db.CRM_SOLUTION_ORG.Where(c => c.USER_ID == SessionCurrentUser.ID || Users.Contains(c.USER_ID.Value)).Select(c => c.ID).ToList();
//Get related solutions ID with the OrgList
List<decimal> SolutionList = db.CRM_SOLUTION_OWNER.Where(p => OrgList.Contains(p.SOLUTION_ORG_ID.Value)).Select(c => (decimal)c.SOLUTION_ID).Distinct().ToList();
return SolutionList;
}
請參閱[使用ToList()時是否會影響性能?](http://stackoverflow.com/questions/15516462/is-there-a-performance-impact-when-calling-tolist)。 – mason
最簡單的事情就是簡單地刪除「ToList()」,儘管這可能會或可能不會解決您的問題。調用ToList()會導致整個IEnumerable/IQueryable立即解析而不是懶惰地解析。 – Casey