c# - Check if list of objects single property value exists in Hashset -


i have list of objects in objects have guid id property.

i have hashset containing bunch of guids.

what fastest way check if each objects guid in list exists in hashset, , update property on object in list if exist? have ability change hashset different data type if needed, list must remain same.

here's classes/enumerable

public class test { public guid id {get; set;} public bool isresponded {get; set;} }  var clientresponses = new hashset<guid>();  var testrecords = new list<test>(); 

this doing

  foreach (var test in testrecords)     {         if (clientresponses.contains(test.id))             test.isresponded = true;     } 

you can so

foreach (var test in testrecords) {     if (clientresponses.remove(test.id))         test.isresponded = true; } 

or, more briefly

foreach (var test in testrecords) {     test.isresponded = clientresponses.remove(test.id); } 

every found value removed hashset, each next iteration faster. of course, it's worth large volumes of data. , besides, necessary recreate hashset.

also can try optimization (it assumed properties isresponded false default)

foreach (var test in testrecords) {     if (clientresponses.remove(test.id))     {         test.isresponded = true;         if (clientresponses.count == 0)             break; // remaining isresponded values remain unchanged      } } 

this approach advantageous if size of testrecords collection larger size of hashset , high probability values hashset coincide values in collection. in case of finding all, there no reason continue iterate on collection. so, break loop.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -