Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select items from IEnumerable

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading