c# - Merge two lists of objects which hold different data -
I have an object Project
which has many fields, one of which is names < / Code>. I have a spreadsheet of projects in which some of these fields are included, and the rest in the other. However, both spreadsheets have the
name
field.
I have read them and two list & lt; Project & gt;
, the popularity of the available field is made popular that looks like a project from a special source such as List 1:
{name = "MyPoraj", type = " Form ", priority = null}
from where a project list 2:
{name =" myProj ", type = null, priority =" high "}
Now, I want to merge these two lists
How can I get this? Is there a good way to do this well?
Thanks
Maybe to find values or There are hundred types of this type. You can only merge the data from one list to another, to join the When projects can disappear in one of the lists, it is more complex, then none of the lists is complete. It can then be useful to make a complete list of project names in the beginning. ?? Use a operator A dictionary can be useful.
// Put one in a word for fast access dict2 = list2.ToDictionaty (x => x.Name, x); // merge using list ?? Var merged = list 1. Select (x = & gt; {var p2 = dict2 [x.Name]; return new project) {name = x.Name, type = x.Type ?? P2.Type, priority = x. Priority P2.Priority}}); Join lists by name
var merge = list1 // .Join (list2, x => x.Name, x = & Gt; X.Name, (p1, p2) = & gt; new {p1 = p1, p2 = p2}). (X => New project () {name = P1.Name, type = P1.Type ?? p2. Type, priority = P1 primacy? P 2. priority}};
foreach
in a result and process the results. e.t.c.
Comments
Post a Comment