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

huangapple go评论70阅读模式
英文:

How to merge two lists by a unique field value and fill empty fields with values from one list?

问题

 var list3 = (from a in list2
              join b in list1
              on a.Model_id equals b.Model_id
              select new Structure
              {
                 Category = string.IsNullOrEmpty(b.Category) ? a.Category : b.Category,
                 Place = string.IsNullOrEmpty(a.Place) ? b.Place : a.Place,
                 Model_id = a.Model_id,
                 Link = string.IsNullOrEmpty(b.Link) ? a.Link : b.Link
              }).ToList();
英文:

How to merge two lists by unique field value and fill empty fields with values ​​of one of the list?

I have a class where unique field is 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; }
    }

And two filled lists:


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 =""}
  };

Both lists are filled with millions of entries. As we can see, both lists have the same entries with the same Model_id, list2 has more entries than list1 and some fields are not filled in both lists. Need to get a list like this:

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 =""}
  };

Okay, if you do this through a for loop, then the processing procedure takes over two hours.

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]);
       }

I'm tried to use LINQ, but I get result list without records with empty fields values from list2:

           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 });

答案1

得分: 0

为了合并这两个列表并填充空字段,您可以使用LINQ的Join操作,然后选择所需的字段来创建一个新列表。

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();
英文:

To merge the two lists and fill empty fields with values from one of the lists, you can use LINQ's Join operation and then select the desired fields to create a new list.

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();

huangapple
  • 本文由 发表于 2023年5月22日 17:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定