Clearly you can't process the list of dates without iterating it, but you can use Linq to produce the sequence in the first place, like so:
var dateInfo =
Directory.EnumerateFiles(directoryName)
.Select(filename => new FileInfo(filename))
.Select(info => new {info.Name, info.CreationTime});
That'll give you a list of FullName
/CreationTime
pairs, where FullName
is the full path of the file, and CreationTime
is the creation time of the file.
You can process it like so:
foreach (var item in dateInfo)
Console.WriteLine($"{item.FullName} created on {item.CreationTime}");
If you just want the (unique) dates that the files were created on:
var uniqueDates = dateInfo.GroupBy(x => x.CreationTime.Date).Select(y => y.Key);
foreach (var date in uniqueDates)
Console.WriteLine(date);
Finally, if you need the dates to be ordered:
var uniqueDates =
dateInfo.GroupBy(x => x.CreationTime.Date)
.Select(y => y.Key)
.OrderBy(z => z);
(And use .OrderByDescending()
for the reverse order, of course.)
If you prefer Linq query syntax:
var uniqueDates =
from date in dateInfo
group date by date.CreationTime.Date into g
orderby g.Key
select g.Key;
Or putting the entire thing in one Linq query (maybe getting a bit unreadble here, so you might want keep it as separate queries, but this is for completeness):
var uniqueDates =
from date in
from file in Directory.EnumerateFiles(directoryName)
select new FileInfo(file).CreationTime
group date by date.Date into g
orderby g.Key
select g.Key;