Search code examples
c#linq

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


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

Solution

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