Jitendra Jadav
Jitendra Jadav

Reputation: 603

DateDiff in LINQ

How I can find datediff in month using LINQ?

Upvotes: 9

Views: 11550

Answers (2)

EgorBo
EgorBo

Reputation: 6142

var result = from i in myTable 
             select SqlMethods.DateDiffMonth(i.DateStart, i.DateEnd);

This will generate sql query with DATEDIFF function

Upvotes: 29

tzaman
tzaman

Reputation: 47790

If I understand correctly, you want the number of month boundaries crossed between two specific dates. You don't need LINQ for that; this should work:

// Assuming DateTime startDate, endDate
int monthDiff = ((endDate.Year - startDate.Year) * 12) + 
                (endDate.Month - startDate.Month);

Upvotes: 0

Related Questions