Click here to Skip to main content
16,021,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
C#
var files = (from c in rootDir.GetFiles()
                            where c.CreationTime >= DateTime.Now.Date.AddDays(-1)
                            select c).ToList();


the above code is working fine it will return yesterday created files as well as today's but i need to get only yesterday created files, for that if i use == instead of >=
i.e.
C#
var files = (from c in rootDir.GetFiles()
                            where c.CreationTime == DateTime.Now.Date.AddDays(-1)
                            select c).ToList();


it will not returning any files
Posted
Updated 17-Feb-14 1:38am
v2
Comments
Ziee-M 17-Feb-14 6:51am    
I am not 100% sure, but there is an incompatibility issue with Linq and DateTime.

Try to select a list, then filter the data using foreacah instead of the Where clause, i think it will solve tour problem.

You are trying to compare Date + Time values; obviously, dates may match, but not times.

Here's what you could do instead:
C#
var files = (from c in rootDir.GetFiles()
             where (c.CreationTime >= DateTime.Today.AddDays(-1)) && (c.CreationTime < DateTime.Today)
             select c).ToList();


Hope this helps.
 
Share this answer
 
var files = (from c in rootDir.GetFiles()
where c.CreationTime >= DateTime.Now.Date.AddDays(-1) & c.CreationTime < DateTime.Now.Date
select c).ToList();
 
Share this answer
 
SQL
var files = from c in directoryInfo.GetFiles()
            where c.CreationTime >somedate
            select c;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900