473,403 Members | 2,293 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,403 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 7062
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
7
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...
11
by: Alexander Vasilevsky | last post by:
Linq "into" and "let" equally??? http://www.alvas.net - Audio tools for C# and VB.Net developers
4
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.). ...
5
by: Neil | last post by:
Hi group, is there a special C# LINQ group available? Regards
2
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...
4
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...
5
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
3
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: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.