我有一個對象列表,其中的對象有一個Guid Id屬性。檢查Hashset中是否存在對象的單一屬性值列表
我也有一個Hashset包含一堆Guid。
檢查列表中的每個對象Guid是否存在於Hashset中的最快方法是什麼,然後更新列表中Object的另一個屬性(如果存在)?如果需要,我可以將Hashset更改爲其他數據類型,但列表必須保持不變。
這裏的類/枚舉
public class Test
{
public Guid Id {get; set;}
public bool IsResponded {get; set;}
}
var clientResponses = new HashSet<Guid>();
var testRecords = new List<Test>();
這是我目前正在做
foreach (var test in testRecords)
{
if (clientResponses.Contains(test.Id))
test.IsResponded = true;
}
「最快的方式」是指從耗時編碼的角度,還是從應用程序的性能角度? –
這似乎是對我來說最好的方法。一個更好的問題是你的性能要求到底是什麼,這是否符合他們的要求?如果確實如此,則無需嘗試對其進行優化。 – juharr
首先你可以寫'test.IsResponded = clientResponses.Contains(test.Id)'。除此之外,我認爲這是最好的解決方案。 – Toxantron