• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

linq date comparison query

I need a linq query to check two dates.

customerRegDate and customerApprovalDate

The customer customerApprovalDate needs to be within seven days of the customerRegDate.  If it is return true else return false
0
mugsey
Asked:
mugsey
  • 11
  • 5
8 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
i've added the IsEligible check from your previous post:
public string IsOverAge(int CustID)
    {
        DataContext dc = new DataContext();
var temp = from j in dc.customers
                         where j.custid == CustID && j.isEligible == true
select j;

if(temp.Count() == 0) return "NOT APPLICABLE";

        return (from t in temp
                         where t.custid == CustID && t.custRegDate.AddDays(7) < DateTime.Now &&
t.customerApprovalDate <= t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;
    }

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
its possible to narrow down the second linq since we already found the matched custId:
public string IsOverAge(int CustID)
    {
        DataContext dc = new DataContext();
var temp = from j in dc.customers
                         where j.custid == CustID && j.isEligible == true
select j;

if(temp.Count() == 0) return "NOT APPLICABLE";

        return (from t in temp
                        where t.custRegDate.AddDays(7) < DateTime.Now &&
t.customerApprovalDate <= t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;
    }

Open in new window

0
 
Meir RivkinFull stack Software EngineerCommented:
again, u need to be aware that the 1st linq might yield no records cause the custID couldn't be found, in that case "NOT APPLICABLE" will be return.

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
mugseyAuthor Commented:
OK thanks

Just to clarify as I am a newbie

The customerApprovalDate cannot be more than 7 days more than the  customerRegDate  - is the following correct

where t.custRegDate.AddDays(7) < DateTime.Now &&
t.customerApprovalDate <= t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;


0
 
Meir RivkinFull stack Software EngineerCommented:
if the customerApprovalDate should be within a week from customerRegDate:

where t.custRegDate.AddDays(7) < DateTime.Now &&
t.customerApprovalDate.AddDays(7)  < t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;
0
 
Meir RivkinFull stack Software EngineerCommented:
if custRegDate should be within a week from current date and customerApprovalDate should be within a week from custRegDate then the right linq is:

where t.custRegDate.AddDays(7) >= DateTime.Now &&
t.customerApprovalDate.AddDays(7)  >= t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;
0
 
mugseyAuthor Commented:
Thanks

One point I just noticed is that the customerApprovalDate is nullable and I cannot get

t.customerApprovalDate.AddDays(7)

What is the best way round this
0
 
Meir RivkinFull stack Software EngineerCommented:
to first check if it is not null (using HasValue property) and then continue with the linq expression inside the clause:

where t.custRegDate.AddDays(7) >= DateTime.Now &&
t.customerApprovalDate.HasValue &&
t.customerApprovalDate.AddDays(7)  >= t.custRegDate
                         select t).Count() > 0 ? "Yes" : "No;
0
 
mugseyAuthor Commented:
Hi thanks

However because customerApprovalDate is nullable I cannot get the

t.customerApprovalDate.AddDays(7)
0
 
Meir RivkinFull stack Software EngineerCommented:
did u try my posted code?
0
 
mugseyAuthor Commented:
Could I do


t.customerApprovalDate <= t.custRegDate.AddDays(7)

So I am checking that the "customerApprovalDate" cannot be more than 7 days after the custRegDate



0
 
Meir RivkinFull stack Software EngineerCommented:
yes
0
 
mugseyAuthor Commented:
I did try your code but I cannot get intellisence for ADDAYS
0
 
Meir RivkinFull stack Software EngineerCommented:
t.customerApprovalDate <= t.custRegDate.AddDays(7)
is equal to
t.customerApprovalDate.AddDays(7)  >= t.custRegDate
0
 
Meir RivkinFull stack Software EngineerCommented:
if custRegDate is typeof DateTime u can use casting:

t.customerApprovalDate <= ((DateTime)t.custRegDate).AddDays(7)
0
 
Meir RivkinFull stack Software EngineerCommented:
if it is string u can use DateTime.Parse:

t.customerApprovalDate <= DateTime.Parse(t.custRegDate.ToString()).AddDays(7)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now