Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data between two dates using LINQ

Tags:

c#

.net

linq

How to filter data between two datetime. Here i am filtering the text file length in a directory..I need to filter text file between the selected date.

DateTime startDate = dateTimePicker1.Value;
DateTime endDate = dateTimePicker2.Value;
var queryList1Only = from i in di.GetFiles("*.txt", SearchOption.AllDirectories)  
                     select i.Length;

Any suggestion?

like image 438
bala3569 Avatar asked Jun 01 '11 10:06

bala3569


3 Answers

Use the Where clause:

DateTime startDate = dateTimePicker1.Value;
DateTime endDate = dateTimePicker2.Value;

var queryList1Only = from i in di.GetFiles("*.txt", SearchOption.AllDirectories) 
                     where i.GetCreationTime() > startDate && i.GetCreationTime() < endDate
                     select i.Length;

Instead of GetCreationTime you could use GetLastWriteTime or GetLastAccessTime.

I'd advise checking out a few examples using the where clause for a full understanding of how it all works here.

like image 62
Lloyd Powell Avatar answered Sep 22 '22 15:09

Lloyd Powell


Well, how about a where clause?

var query = from i in di.GetFiles("*.txt", SearchOption.AllDirectories) 
            where (i.GetCreationTime() > startDate && i.GetCreationTime() < endDate)
            select i.Length;
like image 22
Christoffer Avatar answered Sep 19 '22 15:09

Christoffer


from fi in new DirectoryInfo(@"c:\path").EnumerateFiles("*.txt")
where fi.CreationTime > startDate and fi.CreationTime < endDate)
select fi.FullName;
like image 39
abatishchev Avatar answered Sep 20 '22 15:09

abatishchev