|
|
|
|
|
by wolfgang42
3250 days ago
|
|
Take the following code, which I had occasion to write this morning: foreach (var item in items) {
if (items.Where(item2 => item.Upc == item2.Upc).Count() != 1) {
item.Duplicate = true;
}
}
This is the most readable and straightforward code I could come up with for the task; it's also not particularly efficient, since it does at least twice as many comparisons as necessary (more if there are actually duplicate items). To make it more efficient would require extra loops and manually indexing them to skip over comparisons that had already made; in this context I decided against this because the performance impact was minimal since there are other far less efficient portions of the code which would best be addressed first if the program turns out to be too slow. |
|
Note that this was conceived in just a few minutes of reading your code, deciphering syntax only remotely familiar, and figuring out what was it supposed to do, all with cold start (I haven't written much today yet). Something went wrong with your thought process.