473,218 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Linq. Date Range


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?

Sep 6 '08 #1
1 7054
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

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?).

Sep 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
by: =?Utf-8?B?Q2hha3JhdmFydGh5?= | last post by:
Today, after watching the presentation by Amanda Silver at http://channel9.msdn.com/Showpost.aspx?postid=335058 , from Channel 9, started exploring the LINQ features. Surprisingly, few facts...
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
by: BeSharp | last post by:
I recently stumbled across a pretty interesting LINQ to SQL question and wonder, whether anybody might have an answer. (I'm doing quite some increasing LINQ evangelism down here in Germany.). ...
by: Neil | last post by:
Hi group, is there a special C# LINQ group available? Regards
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible...
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
by: Mythran | last post by:
Consider the following: List<intints = new List<int>(); for (int i = 0; i < 20; i++) { ints.Add(i); } // EXAMPLE 1 var strings = from i in ints
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.