>
>
>
V3220. The result of the LINQ method wi…


V3220. The result of the LINQ method with deferred execution is never used. The method will not be executed.

The analyzer has detected that the result of the LINQ method with deferred execution is not executed.

The example:

public static void PrintNames(List<string> names, char startLetter)
{
  int it = 0;
  var filteredNames = names.Where(name => 
                      {
                        ++it;
                        Console.WriteLine($"{it}) {name}");
                        return name.StartsWith(startLetter); 
                      });

  Console.WriteLine($"Type: '{filteredNames.GetType()}'");
  Console.WriteLine(String.Join(',', names));
}

A delegate that both applies a filter condition and prints elements along with their positions from the original collection to the console is passed to the Where method.

However, neither the output of names with positions nor filtering will occur. Instead of filteredNames, names is passed to theString.Join method. As a result, the filtered collection is not printed to the console, and filtering does not occur at all. This behavior occurs because when Where is called, filtering is not executed immediately but is deferred. So, the delegate code will not be executed until the collection is iterated over.

You can learn more about deferred execution here.

Note that after calling Where via the GetType method, the collection type is printed. When this method is called, iteration over the collection is not executed, which means that the delegate passed to Where will not operate either.

To fix this error, pass filteredNames instead of names to String.Join:

public static void PrintNames(List<string> names, char startLetter)
{
  int it = 0;
  var filteredNames = names.Where(name => 
                      {
                        ++it;
                        Console.WriteLine($"{it}) {name}");
                        return name.StartsWith(startLetter); 
                      });

  Console.WriteLine($"Type: '{filteredNames.GetType()}'");
  Console.WriteLine(String.Join(',', filteredNames));
}

When String.Join is called, filteredNames is iterated over, triggering the collection filtering. So, the delegate code will be executed, and the elements with positions as well as the filtered collection are printed to the console.