so using LINQ attempting to compare 2 values in the DB and select where the date is less than exactly a year today…so anything between 24/1/12 & 24/1/13 will be selected.
So i’ve got…
var selectedObject =
(from workstation in db.Work_Station
join invoice in db.Invoices on workstation.id equals invoice.Site_Id
where InvoiceDate < DateTime.Now.AddYears(-1)
select workstation).Distinct().ToList();
this is producing a list but its not correct.
I’ve been playing about with the add years but can’t seem to get it..
Anyone have any ideas?
Thank You
EDIT:
Hey everyone thank you for the quick response…i actually made a slight mistake, I want to return the value which has an invoice date of greater than exactly a year today…..So anything dated <= 24/1/12 will be returned. i have used ur suggestions to use…
DateTime OneYearAgo = DateTime.Now.AddYears(-1);
var selectedObject =
(from workstation in db.Work_Station
join invoice in db.Invoices on workstation.id equals invoice.Site_Id
where InvoiceDate <= OneYearAgo
select workstation).Distinct().ToList();
This returns a number of invoices…some of which are before the 23/1/12 and some of which are after.
Iv tried changing to where CurrentInvoiceDate >= OneYearAgo, which should return any dated between now and one year ago, but nothing is returned…what am i doing wrong? please advise…thank you
Your query matches your textual explanation. But both don’t match your sample data.
What you want is all data where the date is greater than or equal (not less than) a year ago:
If it is possible that you have invoices with a date in the future, you also need to specify that the invoices should have a date less than or equal to today:
BTW: You want to use
DateTime.Todayinstead ofDateTime.Now.Using DateTime.Now would not return invoices from the 24/1/12 that don’t have a time or that have a time earlier than now.