bala3569
bala3569

Reputation: 11010

Filter data between two dates using 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?

Upvotes: 7

Views: 18194

Answers (4)

Cesar Alvarez
Cesar Alvarez

Reputation: 89

this work for me

var data = (from dataperiod in periods where dataperiod.DateStart <= (DateTime)finalDayNovelty && dataperiod.DateEnd >= (DateTime)initialDayNovelty select dataperiod ).ToList();

Upvotes: -1

abatishchev
abatishchev

Reputation: 100248

from fi in new DirectoryInfo(@"c:\path").EnumerateFiles("*.txt")
where fi.CreationTime > startDate and fi.CreationTime < endDate)
select fi.FullName;

Upvotes: 2

Lloyd Powell
Lloyd Powell

Reputation: 18770

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.

Upvotes: 6

Christoffer
Christoffer

Reputation: 12910

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;

Upvotes: 5

Related Questions