1

I have a two lists of class object :

private List<IntVector> UserData = new List<IntVector>();
private List<IntVector> EditData = new List<IntVector>();

UserData is the Master List and EditData is another list which would be used to add, delete, or modify the list. One of the class memeber is CaseNo which would not change for an IntVector.

I have made changes in the EditData and would like to merge the changes to the UserData.

Any idea how to do it ?

EDIT : Here is the definition of IntVector

class IntVector
    {
        //private DateTime date_kept;
        private string date1;
        private string next_date;
        private string presence;
        private string next_hearing_time;


        //private Hashtable case_type;
        private string court_name;
        private string case_no;
        private string status;
.
.
.
        public string Date1
        {
            set { this.date1 = value; }
            get { return this.date1; }
        }

        public string NextDate
        {
            get { return this.next_date; }
            set { this.next_date = value; }
        }

}
Kiran
  • 8,034
  • 36
  • 110
  • 176
  • dup: http://stackoverflow.com/questions/4488054/merge-two-or-more-lists-into-one-in-c-sharp-net – Cole Tobin Dec 11 '12 at 04:57
  • 1
    Can you show us IntVector body? – Mahdi Tahsildari Dec 11 '12 at 04:57
  • 1
    It is not clear why you need the separate lists. Why not just edit UserData or make EditData only have the entries which were edited? – Trisped Dec 11 '12 at 04:58
  • Well, UserData is not modified Only Changes are committed to it. The changes ( both addition, deletion, modification) would reflect in EditData – Kiran Dec 11 '12 at 05:04
  • @KiranChandrashekhar if the difference between `addition, deletion and modification` is important then I'm afraid you need more than just a simple `Concatenation`, for each item you shoud check to see if it is in `addition` state to be added or ... – Mahdi Tahsildari Dec 11 '12 at 05:12
  • The statement "Well, UserData is not modified Only Changes are committed to it." does not make sense to me, are you saying that `UserData` is the complete list and `EditData` is the list of changes which need to be made to `UserData`? In that case for each item in `EditData` you would need to find the matching value in `UserData` and replace it right? – Trisped Dec 11 '12 at 05:19
  • No, EditData would not contain just the changes. To give an analogy, you have a source file in cvs and make the changes and you need to checkin that change. How would you do it. Instead of source file, I have a list of class object. – Kiran Dec 11 '12 at 05:28

2 Answers2

1

Assuming IntVector is a struct, the IList.Union method does what you need by merging a list and removing duplicates:

UserData  = UserData.Union(EditData).ToList();

If it is not a struct.. then you can still do the above, however you'll have to overload the equality operator, Equals and possibly GetHashCode for it.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • So, if ther are changes in EditData, how would Union work ? Which object is selected. If there are changes, they will not be duplicates right ? For union() they are 2 different objects – Kiran Dec 11 '12 at 05:11
0

I think what you are looking for is something like:

UserData = (from u in UserData
                      join e in EditData
                      on u.CaseNo equals e.CaseNo
                      into joined
                      from x in joined.DefaultIfEmpty()
                      select new IntVector
                      {
                          CaseNo = u.CaseNo,
                          Data = x == null ? u.Data : x.Data
                      }).ToList();
Jake
  • 82
  • 3