Merge and Update Two Lists in C#

后端 未结 6 1220
一生所求
一生所求 2021-01-18 07:12

I have two List objects:

For example:

List 1:
ID, Value where Id is populated and value is blank and it contains s

相关标签:
6条回答
  • 2021-01-18 07:53

    use linq: list1=list2.Union(list1);

    0 讨论(0)
  • 2021-01-18 07:56

    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);
      list1[pos].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<T>(this List<T> list1, List<T> list2) 
    where T:SomeIdValueSupertype {
      int pos = 0;
      foreach (var e in list2) {
        pos = list1.FindIndex(pos, x => x.Id==e.Id);
        list1[pos].Value = e.Value;
      }
    }
    
    0 讨论(0)
  • 2021-01-18 07:58

    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.
    
    0 讨论(0)
  • 2021-01-18 07:58
     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);
                }
    }
    
    0 讨论(0)
  • 2021-01-18 08:13
    Dictionary<int, string> List1 = new Dictionary<int, string>();
    List1.Add(1,"");
    List1.Add(2,"");
    List1.Add(3,"");
    List1.Add(4,"");
    List1.Add(5,"");
    List1.Add(6,"");
    
    Dictionary<int, string> List2 = new Dictionary<int, string>();
    List2.Add(2, "two");
    List2.Add(4, "four");
    List2.Add(6, "six");
    
    var Result = List1.Select(x => new KeyValuePair<int, string>(x.Key, List2.ContainsKey(x.Key) ? List2[x.Key] : x.Value)).ToList();
    
    0 讨论(0)
  • 2021-01-18 08:15

    I would probably use a dictionary rather than a list:

        // sample data
        var original = new Dictionary<int, int?>();
        for (int i = 1; i <= 10; i++)
        {
            original.Add(i, null);
        }
        var updated = new Dictionary<int, int>();
        updated.Add(2, 67);
        updated.Add(4, 90);
        updated.Add(5, 98);
        updated.Add(11, 20); // add
    
        // merge
        foreach (var pair in updated)
        {
            original[pair.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.

    0 讨论(0)
提交回复
热议问题