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;
}
}
Esta función se llama muchas veces, por ejemplo, 1000 veces y hay 5000 estudiantes en el conjunto de resultados. ¿Cómo puedo optimizar esta función? Sé que la conversión de IEnumerable
a HashSet
está causando muchos gastos generales. ToHashSet
es mi método de extensión. Esta función es reducir la velocidad y consumir mucho tiempo.Optimizar la conversión de IEnumerable a HashSet en LINQ
¿Qué hace ToHastSet? – Turbot
agregado aHashSet ... el hashset es un código de Internet. – abbas