我需要一些幫助來解決一個小問題:我有一個List a和一個List b。List b包含一個MigrationID,它是List a中物件的ID。現在我想匹配具有相同MigrationID和ID的物件,并從中創建一個包含obj a、obj b和SyncID的新SyncObject。但我不想使用回圈,因為我認為它表現不佳。
例如:
if b.MigrationID = a.ID => create new SyncObejct(SyncID, obj a, obj b)
我的代碼現在看起來有點像這樣:
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
}
串列都充滿了資料。
謝謝!
uj5u.com熱心網友回復:
也許是這樣的:
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
});
}
這result將是一個具有匹配元素的串列

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481209.html
上一篇:使用字串 范圍創建串列
下一篇:如何根據屬性減去兩個物件串列?
