Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:21:52+00:00 2026-06-17T19:21:52+00:00

so using LINQ attempting to compare 2 values in the DB and select where

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:21:53+00:00Added an answer on June 17, 2026 at 7:21 pm

    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:

    var selectedObject =
                (from workstation in db.Work_Station
                 join invoice in db.Invoices on workstation.id equals invoice.Site_Id
                 where InvoiceDate >= DateTime.Today.AddYears(-1)
                 select workstation).Distinct().ToList();
    

    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:

    var selectedObject =
                (from workstation in db.Work_Station
                 join invoice in db.Invoices on workstation.id equals invoice.Site_Id
                 where InvoiceDate >= DateTime.Today.AddYears(-1)
                    && InvoiceDate <= DateTime.Today
                 select workstation).Distinct().ToList();
    

    BTW: You want to use DateTime.Today instead of DateTime.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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to select from a List using a linq expression where the
Using LINQ on collections, what is the difference between the following lines of code?
I'm using System.Linq.Expressions I was attempting to build a simple LambdaExpression that includes a
I'm attempting to write a CompiledQuery using Linq-to-Entities that will replace a stored procedure
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
I am attempting to iterate an IEnumerable of an object using LINQ, and pull
I am attempting to experiment with linq to sql using this site as a
Using LINQ to Entities, how can I determine if any item from a List
using Linq-to-SQL I'd like to prefetch some data. 1) the common solution is to
using LINQ to XML, this is a sample of my XML <shows> <Show Code=456

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.