4 Replies - 10920 Views - Last Post: 06 May 2013 - 08:02 AM

#1 Xna4life  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 21-February 12

select A where A < B in LINQ

Posted 24 January 2013 - 04:17 AM

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 FinalRetentionInvoiceDataSourceID =
            (from worksites in db.Work_Sites
             join invoice in db.Invoices on worksites.id equals invoice.Site_Id
             where CurrentInvoiceDate < DateTime.Now.AddYears(-10)
             select worksites).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

Is This A Good Question/Topic? 0
  • +

Replies To: select A where A < B in LINQ

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: select A where A < B in LINQ

Posted 24 January 2013 - 04:19 AM

Moved to the .NET forum. I can't see this being in any way jQuery or Javascript related.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: select A where A < B in LINQ

Posted 25 January 2013 - 02:39 PM

CurrentInvoiceDate < DateTime.Now.AddYears(-10)



So you are selecting where CurrentInvoiceDate is less than 10 years ago. Doesn't sound like what you want.

If you want to search between two dates, you need to give both dates to the query.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: select A where A < B in LINQ

Posted 25 January 2013 - 02:50 PM

It's not that hard to create a (generic) function to handle the in between test.

Public Module
<Extension>
Public Function IsBetween(Of T As IComparable(Of T))( Value As T,
                                                 LowerBound As T,
                                                 UpperBound As T,
                       Optional InclusiveLower As Boolean = True,
                       Optionak InclusiceUpper As Boolean = False ) As Boolean
  Return     (LowerBound.ComparedTo(Value) <= If(InclusiveLower,0,-1))
     AndAlso (Value.ComparedTo(UpperBound) <= If(InclusiveUpper,0,-1))
     
End Function
End Module


This post has been edited by AdamSpeight2008: 25 January 2013 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 BattlFrog  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-April 12

Re: select A where A < B in LINQ

Posted 06 May 2013 - 08:02 AM

If I understand, you want one years worth of invoices? LINQ does not seem to like having date function within its syntax, so just create the starting date in a variable, then pass it in:

DateTime lastYear = DateTime.Now.AddYears(-1);
var query = db.CurrentInvoiceDate.Where(x => x.CreateDate > lastYear).ToList();


And I switched the less than, to a greater than, because you the invoice date to be newer than one year ago. Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1