Reputation: 52
Basically I have two XML files that are being deserialized into objects. What I want to do is take the two objects and merge info from both of them based on matching ID's.
The only way I can think of doing this looks like this:
foreach(Computer PC in Computers)
{
foreach(Info data in InfoTable)
{
if(PC.ID == data.ID){PC.Info = data.Info;}
}
}
I have about 10k records so I can't imagine this is the most efficient way.
Upvotes: 1
Views: 55
Reputation:
you can use linq
model pc =(from cm in Computers
join it in InfoTable on
cm.ID equal it.ID
select new model
{
info=it.info
});
where model is same class as computers or your final data
Upvotes: 0
Reputation: 125620
Create a mapping Info.ID
-> Info
using Dictionary<int, Info>
:
var map = InfoTable.ToDictionary(data => data.Id);
and then you need just one loop:
foreach(Computer PC in Computers)
{
if(map.ContainsKey(PC.ID))
{
PC.Info = map[PC.ID].Info;
}
}
Key-lookup in Dictionary<TKey, TValue>
is O(1) operation.
Upvotes: 5