public HashSet<Student> GetStudents(int studentId)
{
IEnumerable<Student> studentTypes = this.studentTypes .Where(x => (x.studentID== studentId));
if (studentTypes .FirstOrDefault() != null)
{
//return new HashSet<Student>(studentTypes);
return studentTypes.ToHashSet();
}
else
{
return new HashSet<Student>();
}
}
public static class LinqUtilities
{
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> enumerable)
{
HashSet<T> hashSet = new HashSet<T>();
foreach (var en in enumerable)
{
hashSet.Add(en);
}
return hashSet;
}
}
Ta funkcja jest wywoływana wiele razy razy mówią 1000 razy i istnieje 5000 uczniów w zestawie wyników. Jak mogę zoptymalizować tę funkcję ... Wiem, że konwersja z IEnumerable
na HashSet
powoduje wiele kosztów ogólnych. ToHashSet
jest moją metodą rozszerzenia. Ta funkcja ma spowolnić i pochłania dużo czasu.Optymalizacja konwersji IEnumerable do HashSet w LINQ
Co robi ToHastSet? – Turbot
dodano doHashSet ... hashset to kod z Internetu. – abbas