c# - List<T> item remove problem -


Do not be afraid of the detailed code. The problem is general I have provided the code to better understand this problem.

I am trying to find a standard approach to manipulation of tables with many relationships and I'm almost done. Here are the M relations with the teacher and course . I have designed my classes as follows:

Teacher - class:

  public class teacher {public id id {get; Set;} public string teacher name {get; Set;} private list & lt; Course & gt; _items = null; Public listing & lt; Curriculum & gt; Before receiving {(if (_items == NULL) {_items = course. Gate Carsbaiteheyraidi (_ID);} return _aytms;} set {_items = value}}} public int Save () {// ... Korstaijr. Delteksbaiteheyraidi (TC, ID); course teacher. save Corrsstekaiaidiaidi (TC, ID, Yhkaitims); // ...} public hair (update) {// ... Korstaijr. Deltekars Bayticirid (TC, Yhkaidi ); Course Teacher. Save CounterSteakIIDID (TC, this.id, this.item); // ...} public static Get the teacher (integer ID) {// ... item.Items = CourseTeacher.GetCoursesByTeacherID (TC, item.ID); // ...} public static list & gt; Teacher & gt; get () {/ / ... before [i] = Kavshy Korsticr. Getkorsejh Baitecraidi (TC items [i] Kaidi); // ...} public static list & gt; teacher & gt; GetTeachersByCourseID (int id) {// ... items = CourseTeacher.GetTeachersByCourseID (TC, id); // ...} public bool Delete () {// ... CourseTeacher.DeleteCoursesByTeacherID (TC, ThiskID); // ...}} < / Code> 

course is exactly the same as teacher - class. And the mapping category is as follows:

  Public Class CourseTeacher {Public Integer CourseID {Get; Set;} Public Integrity TeacherID {Get; Set;} public static void SaveCoursesWithTeacherID (TransactionContext TC, integer teacherID, the list & lt; course & gt; items) {} public static void SaveTeachersWithCourseID (TransactionContext TC, integer courseID, the list & lt; teacher & gt; item) { } private void Save (TransactionContext TC) {} public static void DeleteCoursesByTeacherID (TransactionContext TC, integer TeacherID) {} public static void DeleteTeachersByCourseID (TransactionContext TC, int Korsaidi) {} public static list & lt; Teacher & gt; GetTeachersByCourseID (TransactionContext TC, int-course ID) {} Public Static List & lt; Course & gt; GetCoursesByTeacherID (TransactionContext TC, Integer TeacherID) {}}  

Now my problem is, is this code not working?

  teacher professorXyz = Teacher.Get (2); Course CPP = Course Gate (3); Course Java = Course. Gate (2); ProfessorXyz.Items.Remove (CPP); ProfessorXyz.Items.Remove (Java); ProfessorXyz.Update ();  

This is not working because it's probably not searching for a match or returning to the reader's read only list.

How do I get my teacher / course class? this?

There is no exception. No problem with persistence code item is not being removed

Why professorXyz.Items.Contains (CPP);.? returning false

to check

this There is not a direct answer, but ...

Your design is very (very) relational It makes DB easier, but you do not have a proper oo model. You probably should use the datatabase in dataset and calculate the benefits of the relation class.


To take a shot:

  Teacher Professor Xyz = Teacher. Get (2); Course CPP = Course Gate (3);  

I suspect that the CPP course is being loaded twice, and there are 2 examples of that course in memory. By default, the worst results of your design will not be the same 2 examples, and therefore remove does not work you overload equal , == And GethashCode , but it is.
What you really need is a design where there is no more than 1 instance in memory for a given teacher or in the course.

Re-annotation: An MXM relation in O looks like this:

  class teacher {public read-only list & lt; Curriculum & gt; Course = ...; } Classroom Course {Public Only Read List & gt; Teacher & gt; Teacher = ...; }  

This will work a bit more to write a DB, but it solves many other problems.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -