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
mugseyAsked:
Who is Participating?
 
Meir RivkinConnect With a Mentor Full 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:
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 RivkinConnect With a Mentor Full 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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 RivkinConnect With a Mentor Full 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 RivkinConnect With a Mentor Full 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 RivkinConnect With a Mentor Full 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 RivkinConnect With a Mentor Full stack Software EngineerCommented:
t.customerApprovalDate <= t.custRegDate.AddDays(7)
is equal to
t.customerApprovalDate.AddDays(7)  >= t.custRegDate
0
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
if custRegDate is typeof DateTime u can use casting:

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

t.customerApprovalDate <= DateTime.Parse(t.custRegDate.ToString()).AddDays(7)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.