tags:

views:

46

answers:

2

Hi there, I have the following:

var selectedFilesToReplace = new List<string>();
foreach (string file in files) 
{
    selectedFilesToReplace.AddRange(listUploadedFiles
        .Where(x => Path.GetFileNameWithoutExtension(x) == file));
}

that fill the selectedFilesToReplace collection with a set of FULL path files. I need to select only the file name with its extension.

Is this possible in a single Linq expression?

+1  A: 
var fileNameOnly = selectedFilesToReplace.Select(Path.GetFileName);
thecoop
+3  A: 

Like this:

selectedFilesToReplace.AddRange(listUploadedFiles
    .Where(x => Path.GetFileNameWithoutExtension(x) == file))
    .Select(p => Path.GetFileName(p));

In C# 4, you can also write

selectedFilesToReplace.AddRange(listUploadedFiles
    .Where(x => Path.GetFileNameWithoutExtension(x) == file))
    .Select(Path.GetFileName);
SLaks
@SLaks: I have not yet switched C# 4; is it the case that the "Return Type Inference Does Not Work On Method Groups" restriction has been lifted?http://blogs.msdn.com/b/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx
Ani
@Ani: ​​​​​Yes.
SLaks