CSharp/C# >> Linq. Date Range

by shapper » Sun, 07 Sep 2008 05:37:28 GMT

Hello,

I am getting a list of objects where each object has 2 properties:
StartDate and FinishDate.

I want to select only the items where DateTime.Now is in range ...

But following the next rules:
1. DateTime.Now > StartDate if only StartDate is defined
2. DateTime.Now < FinishDate if only FinishDate is defined
3. StartDate < DateTime.Now < FinishDate if only FinishDate is defined

If both dates are undefined then select the record no matter what ...

Is this possible?

I think I might to something in my Linq expression as follows:

...
where Check(StartDate, FinishDate) == true

Where Check would be a function to do the testing ...

Is this the way to go?

Thanks,
Miguel

CSharp/C# >> Linq. Date Range

by Marc Gravell » Sun, 07 Sep 2008 16:43:26 GMT


> I think I might to something in my Linq expression as follows:


If you are only using LINQ-to-objects, then fine. If you are using
LINQ-to-SQL, then you can get a UDF to do the same by exposing the UDF
in the data-context [as composable] (although it might not necessarily
make good use of indexing) - but Entity Framework doesn't support this
usage.

Personally I'd do it long hand:

where (row.StartDate == null || row.StartDate < when)
&& (row.FinishDate == null || row.FinishDate > when)

That should work in any framework (assuming that StartDate and
FinishDate are DateTime?).

Marc

Similar Threads

1. Date Ranges on relative "dates"

2. Range Validator submitting mis-formatted date in firefox/chrome - CSharp/C#

3. Random Date in Range

Hello,

How can I get a random date in a certain date range? For example:

Get random date between 27-02-2007 and 30-09-2008?

Thanks,
Miguel

4. Date Ranges - CSharp/C#

5. MS Access SQL Select command in date range problem

Tsair wrote:
> Select PoHead.*, PoItem.*, PoItem.Tranno as Tranno from PoItem INNER JOIN 
> [PoHead] ON PoItem.Tranno = PoHead.Tranno Where PoHead.TranDate > 
> #5/10/2005# AND PoHead.TranDate < #9/10/2005# AND TRUE ORDER BY 
> PoHead.TranDate, PoHead.SupplierCode, PoHead.ProjectCode, PoItem.StockCode
> 
> When
> 
> Date1 = #5/10/2005#
> 
> Date2 = #9/10/2005#
> 
> the command no record selected but when Date2 = #10/10/2005#, the record for 
> #6/10/2005# will capture.
> 
> What is the problem ?
> 
> 
> 
> 
you should check out if you are using the right date-format mm/dd/yyyy 
or dd/mm/yyyy

the representation in sql can be different from the database

6. Adding Up Percents Across Date Ranges - CSharp/C#

7. Data compression, date range

given

const uint64_t maxDateValue = 6074000998ull;

write decode/encode date range functions

uint64_t encodeDate(uint64_t start, uint64_t stop);
void decodeDate(uint64_t blob, uint64_t* start, uint64_t* stop);

Interesting exercise ;)

Greets

8. Realistic Date Range For Next Delphi Release? - C++ Builder