I am having performance issues with this LINQ Query.
The data is laoded into to this.students already.
Now when I call the GetStudentData function say 1000 times it has a huge overhead.
Is there a way of improving this without changing the LINQ to a loop
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();
}
So here are the results when looping through it 10000 times with my original version
Original Version : 5.6 seconds on the average
New Version @des’s Code with FirstOrDefault : 3.6 seconds
When you use
Whereyou loop through all records which fulfill given conditions, when you useFirstyou just search for first record which fullfills condition, so usingFirstshould speed it up.