Linq query with month difference between 2 dates

asp.net-mvc-3 c# entity-framework linq

Question

I have 2 Field FromDate and ToDate in database. I want the result LINQ query for records that have month difference of 2 dates with some dynamic value.

Actually I have one database table as EmploymentHistory with field NameOfCompany as nvarchar(50), ProfileID as bigint, EmoploymentFrom as datetime, EmploymentUpto as datetime, IsCurrentEmployer as bit.

ProfileID is reference key of Profile I want to generate a query for all profiles by experience in month.

if IsCurrentEmployer is true then EmploymentUpto is null.

1
1
8/21/2012 5:39:08 AM

Accepted Answer

If you're under .NET 4+ you could try EntityFunctions.DiffMonths utility method e.g.:

var query = query
            .Where(i=> 
                 EntityFunctions.DiffMonths(i.FromDate, i.ToDate) == 2
            );
12
8/10/2012 12:43:59 PM

Popular Answer

Asuming you are using entity framework.. u may

Query(q => q.ToDate < DateTime.Today && q.FromDate > DateTime.Today.AddMonths(-2))

Query is any Iqueryable.. could be select, first, firstOrDefault.. of course you can change DateTime.Today to yours params.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow