0
votes

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

2

2 Answers

3
votes

Select is intended for projecting data, not for performing side-effects.

Simply use a foreach loop:

foreach(FileInfo fileInfo in duplicatedFiles)
{
    fileInfo.Delete();
}
2
votes

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());