如何按唯一字段值合并两个列表,并用其中一个列表的值​​填充空字段?

我有一个类,其中唯一的字段是Model_id:

 class Structure
    {
        public string Category { get; set; }
        public string Place { get; set; }
        public string Model_id { get; set; }    
        public string Link { get; set; }
    }

和两个填好的名单:


List<Structure> list1 = new List<Structure>()
  {
   new  Structure{  Category="nwctrinity",Place ="",Model_id ="00001q", Link ="long_link1"},
   new  Structure{  Category="nwczion",Place ="",Model_id ="00002q",Link ="long_link2"}
  };

List<Structure> list2 = new List<Structure>()
  {
   new  Structure{  Category="",Place ="nebuchadnezzar",Model_id ="00001q", Link =""},
   new  Structure{  Category="",Place ="earth",Model_id ="00002q",Link =""},
   new  Structure{  Category="",Place ="space",Model_id ="00034q",Link =""}
  };

这两份榜单上都有数百万个条目.正如我们所看到的,两个列表都有相同的条目,具有相同的Model_id,List2的条目比List1多,并且一些字段没有在两个列表中填充.我需要一份这样的 list :

List<Structure> list3 = new List<Structure>()
  {
  new  Structure{  Category="nwctrinity",Place ="nebuchadnezzar",Model_id ="00001q", Link ="long_link1"},
  new  Structure{  Category="nwczion",Place ="earth",Model_id ="00002q",Link ="long_link2"},
  new  Structure{  Category="",Place ="space",Model_id ="00034q",Link =""}
  };

好的,如果您通过for循环执行此操作,则处理过程将花费两个多小时.

List<Structure> list3 = new List<Structure>();
   for (int i = 0; i < list2.Count; i++)
       {
        int index = list1.FindIndex(a => a.Model_id == list2[i].Model_id);
        if (index != -1)
          {
           list3.Add(new Structure { Category = list1[index].Category, Model_id = structure[i].Model_id, Place = structure[i].Place, Link = list1[index].Link });
          }
        else list3.Add(list2[i]);
       }

我try 使用LINQ,但从列表2得到的结果列表中没有带有空字段值的记录:

           var invar = (from a in list2
                        join b in list1
                        on a.Model_id equals b.Model_id
                        select new
                          {
                           a.Model_id,
                           a.Place,
                           b.Category,
                           b.Link });

推荐答案

要合并这两个列表并用其中一个列表中的值填充空域,可以使用LINQ的联接操作,然后 Select 所需的域来创建新列表.

var list3 = (from a in list2
             join b in list1 on a.Model_id equals b.Model_id into temp
             from b in temp.DefaultIfEmpty()
             select new Structure
             {
                 Category = b?.Category ?? a.Category,
                 Place = b?.Place ?? a.Place,
                 Model_id = a.Model_id,
                 Link = b?.Link ?? a.Link
             }).ToList();

Csharp相关问答推荐

VB.Net的SON模式导致集合代码不工作

为什么使用DXGI输出复制和Direct 3D时捕获的图像数据全为零?

如何创建ASP.NET Core主机并在同一进程中运行请求

如何在Visual Studio代码中更改大括号模式{},用于C#语言

如何在C#中将对象[*,*]直接转换为字符串[*,*]?

自动映射程序在GroupBy之后使用项目

如何使datagridview的列具有响应性,以便不是所有列都更改其宽度

在路由中使用枚举

应用程序重新启动后,EFCore列表的BSON反序列化错误

如何使用C#获取FireStore中的列表输出文档

C#阻塞调用或await calling inside calling方法

未在数据流块之间传播完成

为什么方法的值在SELECT方法中不会更改?

链接到字典字符串.拆分为(.Key,.Value)

我可以查看我们向应用程序洞察发送了多少数据吗?

C#LINQ延迟执行和嵌套方法

在C#中,当输入一个方法/局部函数时,我的IEnumerator被重置/重新创建.为什么?

EF Core:如何对关系属性进行建模?

如何将默认区域性更改为fr-FR而不是en-US?

C#System.Commandline:如何向命令添加参数以便向其传递值?