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;
}
}
Chức năng này được gọi là rất nhiều lần cho 1000 lần và có 5000 học sinh trong tập kết quả. Làm cách nào để tối ưu hóa chức năng này ... Tôi biết rằng việc chuyển đổi từ IEnumerable
sang HashSet
đang gây ra rất nhiều chi phí. ToHashSet
là phương pháp tiện ích mở rộng của tôi. Chức năng này làm chậm và ăn nhiều thời gian.Tối ưu hóa IEnumerable thành chuyển đổi HashSet trong LINQ
ToHastSet làm gì? – Turbot
đã thêm vàoHashSet ... mã băm là mã từ internet. – abbas