471,330 Members | 1,208 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Linq. Date Range

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
Sep 6 '08 #1
1 6960
I think I might to something in my Linq expression as follows:
...
where Check(StartDate, FinishDate) == true
Is this the way to go?
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
Sep 7 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by dfetrow410 | last post: by
7 posts views Thread by =?Utf-8?B?Q2hha3JhdmFydGh5?= | last post: by
11 posts views Thread by Alexander Vasilevsky | last post: by
4 posts views Thread by BeSharp | last post: by
5 posts views Thread by Neil | last post: by
4 posts views Thread by =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post: by
5 posts views Thread by Mythran | last post: by
3 posts views Thread by =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.