C# 在C语言中合并和更新两个列表#

C# 在C语言中合并和更新两个列表#,c#,.net,linq,list,generic-list,C#,.net,Linq,List,Generic List,我有两个列表对象: 例如: 列表1: ID,值,其中ID填充,值为空,它包含从1到10的ID。 1、“ 2、“ … 10,“” 列表2: ID、Value和其他属性都用值填充,但此列表是列表1中ID的子集。(例如仅3项) 2,67 4,90 5,98 我想要的是一个合并列表1,但具有更新的值。是否有人有任何好的扩展方法来执行此操作,或者有任何elegent代码来执行此操作。最后名单应为: ID,值 1、“ 2,67//列表2中的值 3、“ 4,90 5,98 6.“ … 10,“这是O(m*n)

我有两个
列表
对象:

例如:

列表1:
ID,值,其中ID填充,值为空,它包含从1到10的ID。
1、“
2、“

10,“”

列表2:
ID、Value和其他属性都用值填充,但此列表是列表1中ID的子集。(例如仅3项)
2,67
4,90
5,98

我想要的是一个合并列表1,但具有更新的值。是否有人有任何好的扩展方法来执行此操作,或者有任何elegent代码来执行此操作。最后名单应为:

ID,值
1、“
2,67//列表2中的值
3、“
4,90
5,98
6.“

10,“

这是O(m*n),但应该对任意列表执行此操作

        foreach (var record in List1)
        {
            var other = List2.FirstOrDefault(x => x.Key == record.Key);
            if(other != null) record.Value = other.Value;
        }
如果保证列表是有序的,那么它可以降低到O(n),代价是需要更多的代码。算法是

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.

我可能会使用字典而不是列表:

    // 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);
    }
//示例数据
var original=新字典();
对于(int i=1;i x.Key))
{
Console.WriteLine(pair.Key+“:”+pair.Value);
}
如果您谈论的是对象的属性,这将更为棘手,但仍然可行。

如果两个列表都按ID排序,则可以使用经典合并算法的变体:

int pos = 0;
foreach (var e in list2) {
  pos = list1.FindIndex(pos, x => x.Id==e.Id);
  list1[pos].Value = e.Value;
}
注意,这也要求
list2
在ID方面是
list1
的严格子集(即
list1
实际上包含
list2
的所有ID)

当然,您也可以将其包装在扩展方法中

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;
  }
}
publicstaticvoidupdatewith(此列表列表1,列表列表2)
其中T:SomeIdValueSupertype{
int pos=0;
foreach(列表2中的变量e){
pos=list1.FindIndex(pos,x=>x.Id==e.Id);
列表1[pos].Value=e.Value;
}
}

使用linq:list1=list2.Union(list1)

字典列表1=新字典();
清单1.添加(1,“”);
清单1.添加(2,“”);
清单1.添加(3,“”);
清单1.添加(4“”);
清单1.添加(5“”);
清单1.添加(6“”);
字典列表2=新字典();
清单2.添加(2,“两”);
清单2.添加(4,“四”);
清单2.添加(6,“六”);
var Result=List1.Select(x=>newkeyvaluepair(x.Key,List2.ContainsKey(x.Key)?List2[x.Key]:x.Value)).ToList();

您的值是字符串还是整数?您似乎在列表1中显示字符串,而在列表2中显示int。请在此处提供一些解释。删除不必要的注释代码。我用两个类型的列表尝试了你的代码
list
学生类看起来像这样
student student=new student(){Id=1,Name=“foo”}
使用以下语法
list1=list2.ToList().Union(list1.ToList()).ToList()但它没有合并两个列表;它只是把它们粘在一起,就像一个胶状物。您确定它将列表与更新的值合并吗?我发现了这个关于如何合并的问题,它确认只有当类超过了
GetHashCode()
Equals()
 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);
            }
}
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();