Reputation: 8350
I have a search page, where the articles can be searched by date. The search criteria is only with month and year. So whenever the month is selected and clicked "filter" I take the start day of that month as "dtStartDate" and end day of the month as "dtEndDate"
The Articles which has to searched also has Effective Start date as "EffStartDate" and Effective End date as "EffEndDate"
So what should be the condition if, Both these "EffStartDate" and "EffEndDate" falls inside "dtStartDate" and "dtEndDate" ?
I gave a try with this, but the results are not correct,
If (EffStartDate >= dtStartDate & EffStartDate <= dtEndDate)
//Do Something
ElseIf (EffEndDate >= ddl_FromDate & EffEndDate <= dtEndDate.AddDays(1)) Then
//Do Something
End If
Scenario : Lets assume that there are two articles which are going to be searched using different dates.
Article - 1
EffStartDate = 11/7/2012
EffEndDate = 31/8/2012
Article - 2
EffStartDate = 1/7/2012
EffEndDate = 28/9/2012
Now, in my search page, when i search for the article between 1/07/2012 to 31/07/2012 I should see both the articles as result, because both the effstardate and effenddate somewhere falls in between the dates used for search. But now i see only Artilce - 1 as result. Why ?
Upvotes: 0
Views: 2476
Reputation: 415850
It's clear to me from your example that the text of your criteria is wrong. The text of your criteria state that you only want to show an article if both the effective start and end dates fall within the date range... if the entire effective range of the article fits entirely within the search. But your example indicates that you want to show the article if any part of the date range fits within the range of search. An edit to clarify that would be helpful, but for now I will move forward assuming that the example is the correct interpretation.
You also seem to be mixing language syntax from VB.Net and C# together. You can't just mix syntax like that. The strongest influence in your code snippet seems to be VB.Net, so I'll use that in my example.
In VB.Net, you could write a condition that matches the example for your search like this:
If (EffStartDate >= dtStartDate AndAlso EffStartDate < dtEndDate.AddDays(1)) _
OrElse (EffEndDate >= dtStartDate AndAlso EffEndDate < dtEndDate.AddDays(1))
OrElse (EffStartDate < dtEndDate.AddDays(1) AndAlso EffEndDate >= dtStartDate) Then
'...
End If
I'm also concerned that you're writing this code in an If
block at all. If these dates are in a database, then the database is by far the best place to filter your results. If the data is elsewhere, then a linq-to-objects query of some type will likely yield both better performance and be shorter and easier to maintain.
Upvotes: 2
Reputation: 7505
C# version:
if ((EffStartDate >= dtStartDate && EffStartDate <= dtEndDate) ||
(EffEndDate >= dtStartDate && EffEndDate <= dtEndDate))
{
// Good Article
}
else
{
// Bad Article
}
Upvotes: 0
Reputation: 2151
I didn't get your question well , but like this its simpler
bool isStartEffDateInside=(EffStartDate >= dtStartDate && EffStartDate <= dtEndDate);
bool isEndEffDateInside=(EffEndDate >= ddl_FromDate && EffEndDate <= dtEndDate.AddDays(1));
if(isStartEffDateInside || isEndEffDateInside)
{
// do something global
if(isStartEffDateInside && isEndEffDateInside)
{
//Do something when the two Effs are inside the StartEnd
}
}
hope this helps
Upvotes: 0