c# - How do you use MyList.Contains(MyObject) when NHibernate populates MyList with Proxy Elements -
i have nhibernate many-to-many relationship. before adding item relationship want test whether item exists in collection. add method looks like
public virtual void addcourse(course course) { if (!this.courses.contains(course)) { course.students.add(this); this.courses.add(course); } }
the courses.contains(course) statement fails. i've done digging , realised courses list list of nhibernate proxies not domain objects. sanity knocked test has shown have equality set work correctly. test showed contains doesn't work on subclass.
i've disabled lazy-loading on courses collection , code works fine.
so, how do lazy-loaded objects?
can't test right on machine, have ideas:
check using gethashcode:
if (!this.courses.any(r => r.gethashcode() == course.gethashcode()))
check using unique property of
course
. example, id:if (!this.courses.any(r => r.id == course.id))
both expressions used system.linq
namespace (don't forget include before). imho, second example better one.
Comments
Post a Comment