Merge and Update Two Lists in C?

I would probably use a dictionary rather than a list: sample data var original = new Dictionary(); for (int I = 1; I (); updated. Add(2, 67); updated. Add(4, 90); updated.

Add(5, 98); updated. Add(11, 20); // add // merge foreach (var pair in updated) { originalpair. Key = pair.

Value; } // show results foreach (var pair in original. OrderBy(x => x. Key)) { Console.

WriteLine(pair. Key + ": " + pair. Value); } If you are talking about properties of an object, it will be trickier, but still doable.

I would probably use a dictionary rather than a list: // sample data var original = new Dictionary(); for (int I = 1; I (); updated. Add(2, 67); updated. Add(4, 90); updated.

Add(5, 98); updated. Add(11, 20); // add // merge foreach (var pair in updated) { originalpair. Key = pair.

Value; } // show results foreach (var pair in original. OrderBy(x => x. Key)) { Console.

WriteLine(pair. Key + ": " + pair. Value); } If you are talking about properties of an object, it will be trickier, but still doable.

Thanks, this is what I was looing for.... – chugh97 Aug 19 '09 at 10:44.

This is O(m*n) but should do the job for arbitrary lists foreach (var record in List1) { var other = List2. FirstOrDefault(x => x. Key == record.

Key); if(other! = null) record. Value = other.

Value; } If the lists are guaranteed ordered, then it could be brought down to O(n) at the cost of more code. The algortihm would be Current items start as head of each list While items remain in both lists If the current item of list1 has lower key than list2 advance to next in list1 else if the current item of list2 has lower key than list1 advance to next in list2 else copy value from current list2 item into list1 item and advance both lists.

If you have both lists sorted by ID, you can use a variation of the classical merge algorithm: int pos = 0; foreach (var e in list2) { pos = list1. FindIndex(pos, x => x. Id==e.Id); list1pos.

Value = e. Value; } Note that this also requires list2 to be a strict subset of list1 in terms of ID (i.e. List1 really contains all ids of list2) Of course you can also wrap this in an extension method public static void UpdateWith(this List list1, List list2) where T:SomeIdValueSupertype { int pos = 0; foreach (var e in list2) { pos = list1.

FindIndex(pos, x => x. Id==e.Id); list1pos. Value = e.

Value; } }.

Private void btnSearch_Click(object sender, EventArgs e) { String searchBy = cmbSearchBy.Text.ToString(); String searchFor = txtSearchFor.Text.Trim(); var List3 = (from row in JobTitleDB. JobList where (row.JID.ToString()+row.JobTitleName.ToString().ToLower()). Contains(searchFor.ToLower()) select row).ToList(); if (searchBy == "All") { dgJobTitles.

DataSource = null; //dgJobTitles. DataSource = List1; //dgJobTitles. DataSource = List2; //dgJobTitles.

DataSource = List1. Concat(List2); //dgJobTitles. DataSource = List1.

Union(List2); dgJobTitles. DataSource = List3; //dgJobTitles. DataSource=List1.

AddRange(List2); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions