I have created two lists of objects: One is records from an xml and the other is records from the database.The rule is check if the record from an xml exist in the database then exclude.I have thought of two options: First is to loop the list of records from an xml and for each record check if the id exist in the database.Second is creating a list of objects from an xml and a list of objects from the database. Then compare the two list and get the result.Which one is efficient? I'm thinking of the second options because instead of looping and querying the record, each time, if existed in the database why not put them in the lists and compare them by using linq or equality comparer.
Efficient way in comparing two lists
c#;xml;linq;list
Which one is efficient? depends on your definition of efficient and on your data:Which kind of efficient do you mean: less processing time, less memory consumption, less hours needed to implement or maintain?I assume you mean less processing time.The answer depends:if database contain 1 million rows and XML contain only two rows the first let database check is more efficient. if database and XML both have many items then the second in memory solution can be more efficient if data is properly sorted.In 99.99% of all cases processing time efficiency does not matter as long as the code is not executed more than 10 times per second. Is there a big difference if version 2 needs 0.01 seconds while version 1 needs 0.2 seconds?If i where you i would implement the easier coding as let database check until you really measure that this is a performance bottleneck. Be aware of the anti-pattern premature optimisation