Search code examples
c#linqfileinfo

Delete FileInfo using LINQ


I am getting the following message when trying to delete FileInfo:

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Here is my code:

private void btn_Click(object sender, EventArgs e)
        {
            List<FileInfo> duplicatedFiles = Fso.GetAllDuplicatedFiles(@"C:\", true);

            duplicatedFiles.Cast<FileInfo>().Select(f => f.Delete());
        }

Thanks for any help


Solution

  • Select selects something, FileInfo.Delete is void. But a LINQ query should not cause side effects anyway. Apart from that, why do you cast a FileInfo to FileInfo?

    You can use List.ForEach which isn't LINQ:

    List<FileInfo> duplicatedFiles = Fso.GetAllDuplicatedFiles(@"C:\", true);
    duplicatedFiles.ForEach(fi => fi.Delete());