Linq to objects of several operators versus one operator

In Linq for objects, there is some difference in execution between this code:

var changedFileIDs = updatedFiles.Where(file => file.CurrentVersion != file.OriginalVersion).Select(file => file.ID);
var changedVaultFiles = filesToUpdate.Where(x => changedFileIDs.Contains(x.ID));
foreach (var file in changedVaultFiles)
{
    Vault.Upload(file);
}

and this code?

var changedVaultFiles = filesToUpdate.Where(x => updatedFiles.Where(file => file.CurrentVersion != file.OriginalVersion).Select(file => file.ID).Contains(x.ID));
foreach (var file in changedVaultFiles)
{
    Vault.Upload(file);
}
+4
source share
1 answer

No, there is no difference in performance, because one of the features of Linq is delayed execution , in other words, your request will not be executed until the request variable is enumerated in foreachor foror by calling the extension method ToListor ToArray. So, in your first example, you are composing your main request, but you are not going to execute until you go to it.

, LINQ.

:

LINQ , . . , , . , ( ), , .

+2

Source: https://habr.com/ru/post/1651542/


All Articles