I need some help on a little problem: I have a List a and a List b. List b contains a MigrationID which is the ID of the objects in List a. Now I want to match the objects with the same MigrationID and ID and create out of that a new SyncObject which contains obj a, obj b and a SyncID. But i dont want to use loops because i think it´s underperforming.
For example:
if b.MigrationID = a.ID => create new SyncObejct(SyncID, obj a, obj b)
My Code now looks a bit like this:
private class SyncObject
{
private Guid syncID { get; set; }
private ItemA aItem { get; set; }
private ItemB bItem { get; set; }
}
public void SynchObjAToObjB<T, A>() where T : ItemA, new() where A : ItemB, new()
{
List<T> listA aItems;
List<A> listB bItems;
//here mapping
}
The list´s both are filled with data.
Thank you!
CodePudding user response:
Perhaps something like this:
class ItemA
{
public Guid Id { get; set; }
public string MetaData { get; set; }
}
class ItemB
{
public Guid MigrationID { get; set; }
public string MetaData { get; set; }
}
class SyncObject
{
public Guid syncID { get; set; }
public ItemA aItem { get; set; }
public ItemB bItem { get; set; }
}
void Main()
{
// setup example data
var listA = new[]
{
new ItemA { Id = Guid.NewGuid() }, // no match in listB
new ItemA { Id = Guid.NewGuid() }, // match in listB and will be in the result
new ItemA { Id = Guid.NewGuid() } // match in listB and will be in the result
};
var listB = new[] {
new ItemB { MigrationID = listA[1].Id },
new ItemB { MigrationID = listA[2].Id}
};
// the "mapping"
var result = listA.Join(listB, a => a.Id, b => b.MigrationID, (a, b) =>
new SyncObject
{
aItem = a,
bItem = b,
syncID = a.Id
});
}
And the result
will be a list with matching elements