I am trying to find the date for which a file exists in folder and getting an errror while trying to use IEnumerable.
DateTime testDate = ("2021-05-09");
DateTime snapshotDate = Enumerable.Range(0, 30)
.Select(i => DateTime.Parse(@testDate).AddDays(-i))
.FirstOrDefault(s => EXISTS(string.Format(@"/users/{0:yyyy/MM}/testfile_{0:yyyyMMdd}.txt", DateTime.Parse(@testDate).AddDays(-s))));
Please let me what i am missing here
>Solution :
Either remove Select, or format the date:
string testDate = "2021-05-09";
DateTime snapshotDate = Enumerable.Range(0, 30)
.Select(i => DateTime.Parse(@testDate).AddDays(-i))
.FirstOrDefault(s => EXISTS(
string.Format(@"/users/{0:yyyy/MM}/testfile_{0:yyyyMMdd}.txt", s))); // here
Note that since DateTime is struct FirstOrDefault will return default value – 1/1/0001 12:00:00 AM and not a null.