我遇到此LINQ查詢的性能問題。 這些數據已經包含到this.students中。 現在當我調用GetStudentData函數說1000倍時,它有巨大的開銷。平均 5.6秒:通過它10000次,我原來的版本循環優化此LINQ查詢
原始版本時 是否有改善這一點沒有改變LINQ到環路
public Student GetStudentData()
{
IEnumerable<Students> studentTypes = this.students.Where(x => (x.studentsId == studentId && x.StduentType.Equals(studentType)));
if(studentTypes.Count==0) return new Student() { studentid=studentID};
return (Student)studentTypes.First();
}
所以在這裏的一種方式是結果新版本@沙漠的代碼FirstOrDefault
:3.6秒
你真的在運行這個1000嗎?否則,這篇文章是相當合適的:http://www.hanselman.com/blog/BackToBasicsYouArentSmarterThanTheCompilerPlusFunWithMicrobenchmarks.aspx – Killnine
ew,作爲一邊,我真的不喜歡你如何返回一個新的學生,如果沒有符合條件是發現 - 很混亂。 – Didaxis
是的,我正在運行這1000次 – abbas