var fileIO = new FileIO();
var datafiles = _fileIo.GetFileDetails(directory)
.Where(f => !f.FileName.EndsWith(".csv"))
.Where(f => !f.FileName.EndsWith(".xls"))
.Where(f => !f.FileName.EndsWith(".xlsx"));
似乎应该有一种更简洁的语法,允许传递后缀字符串列表。
答案 0 :(得分:11)
那呢?
var fileExtensions = new HashSet<string>() { ".csv", ".xls", ".xlsx" };
var datafiles = _fileIo.GetFileDetails(directory)
.Where(f =>
fileExtensions.Contains(Path.GetExtension(f)));
我们使用Path.GetExtension
从“ test.csv”中获取“ .csv”,然后将其与一组字符串进行比较。
目前,此比较区分大小写:.csv
将匹配,但.cSv
将不匹配。您可以添加StringComparer
来不敏感地处理这种情况:
var fileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".csv", ".xls", ".xlsx" };