英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论