使用GroupBy
方法:
IEnumerable<FileData> dats = FastDirectoryEnumerator
.EnumerateFiles(myDirectory.FullName, "*.zip", SearchOption.AllDirectories)
.Where(f => f.Size / 1024 > 750)
.Where(f => !f.Name.EndsWith(".reg.zip"))
.Where(f => f.Name.StartsWith("2001"))
.GroupBy(f => f.Name)
.Select(g => g.First());
或者在查询语法中:
IEnumerable<FileData> dats =
from f in FastDirectoryEnumerator.EnumerateFiles(…)
where f.Size / 1024 > 750 &&
!f.Name.EndsWith(".reg.zip") &&
f.Name.StartsWith("2001") &&
group f by f.Name into g
select g.First();
这将返回FileData
每个名称的第一个。如果您只想获取唯一Name
值,则实际上要容易一些:
IEnumerable<string> dats = FastDirectoryEnumerator
.EnumerateFiles(myDirectory.FullName, "*.zip", SearchOption.AllDirectories)
.Where(f => f.Size / 1024 > 750)
.Where(f => !f.Name.EndsWith(".reg.zip"))
.Where(f => f.Name.StartsWith("2001"))
.Select(f => f.Name)
.Distinct();
或者在查询语法中:
IEnumerable<string> dats =
(from f in FastDirectoryEnumerator.EnumerateFiles(…)
where f.Size / 1024 > 750 &&
!f.Name.EndsWith(".reg.zip") &&
f.Name.StartsWith("2001") &&
select f.Name)
.Distinct();