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:

  1. check using gethashcode:

    if (!this.courses.any(r => r.gethashcode() == course.gethashcode())) 
  2. 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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -