Ordinamento con formula matematica con NHibernate (C #)
-
10-10-2019 - |
Domanda
public class Feedback
{
public virtual int Id { get; private set; }
public virtual string ContentText { get; set; }
public virtual DateTime FeedbackDate { get; set; }
public virtual Student student { get; set; }
}
Il mio Commenti classe.
public class Student
{
public virtual int Id { get; private set; }
public virtual int NumberOfStars { get; set; }
public virtual IList<Feedback> Feedbacks { get; private set; }
public Student()
{
Feedback = new List<Feedbacks>();
}
}
Il mio allievo del codice categoria
public class Course
{
public virtual int Id { get; set; }
// bla bla bla
public virtual IList<Student> Students { get; private set; }
public Course()
{
Students = new List<Student>();
}
public IList<Student> SortBy(string type)
{
// some other sorting
else if (type.Equals("popular")){
sortedStudents = session.CreateCriteria(typeof(Student))
.CreateAlias("Student", "s")
.CreateAlias("s.Feedback", "f")
.AddOrder(Order.Desc( -------- ))
.List();
}
return (IList<Student>) sortedStudents;
}
}
classe di mio corso
Voglio studenti di ordinamento in un corso con il metodo SortBy: se il tipo è x lo selezionerà con seguente regola (Students.Feedback.Count) * 5 + Student.NumberOfStars)
Come?
Soluzione 2
query con LINQ
IList sortedStudents = (from student in this.Students
where student.Course == this
orderby (student.Feedbacks.Count*3 + student.NumberOfStars)
select student).ToList();
Altri suggerimenti
HQL:
List<Student> sortedStudents = session
.CreateQuery(
@"from Students student
where student.Course == :course
order by size(student.Feedbacks) * 3 + student.NumberOfStars")
.SetEntity("course", course)
.List<Student>();
size
è una funzione HQL. Vedere la capitolo "Espressioni" nella documentazione NH.
Si può anche selezionare con criteri e ordinare con LINQ.
Modifica
appena visto che lo si utilizza in una proprietà e possono avere gli studenti già in memoria. Non hai bisogno di una query, solo per ordinare esso.
return students
.OrderBy(x => x.Feedback.Count * 5 + x.NumberOfStars)
.ToList();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow