Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how sql query convert in Linq [closed]

I am trying to convert an SQL query to Linq

The query is :

 SELECT Distinct(InvoiceID)
 FROM Invoices
 WHERE ((DATEDIFF(day, InvoiceDate, GETDATE()) > PaymentDays)  AND
 status= 'false') 

I have written the following code but not getting proper result....

var outstanding = db.Invoices.Where(t =>
     (t.InvoiceDate.Value.Subtract(DateTime.Today)).Days > t.PaymentDays.Value 
    && !(bool)t.Status)

In my opinion, DATEDIFF doesnt have proper equivalent in LINQ to SQL

like image 821
Ashwin Avatar asked Feb 26 '26 06:02

Ashwin


1 Answers

I see a few problems with the code you've written.

  • LinqToSql wont support DateTime.Subtract, for this sort of operation you'll need SqlMethods.DateDiffDay docs.
  • The string "false" cannot be cast to a boolean, you need to either use bool.Parse or just compare to the string "false" as you did in your original.
  • your original gets a distinct list of invoice id's. This is totally missing from your LINQ attempt. Append .Select(t => t.InvoiceID).Distinct() to your LINQ.
like image 93
Jamiec Avatar answered Feb 27 '26 19:02

Jamiec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!