Linq查询2个日期之间的月差异

use*_*ser 1 c# linq entity-framework asp.net-mvc-3

我在数据库中有2个Field FromDate和ToDate.我希望结果LINQ查询具有2个日期的月差异和一些动态值的记录.

实际上我有一个数据库表作为EmploymentHistory,字段NameOfCompany为nvarchar(50),ProfileID为bigint,EmoploymentFrom为datetime,EmploymentUpto为datetime,IsCurrentEmployer为bit.

ProfileID是Profile的引用键我希望按月经验为所有配置文件生成查询.

如果IsCurrentEmployer为true,那么EmploymentUpto为null.

Ale*_*lex 12

如果你在.NET 4+以下,你可以尝试EntityFunctions.DiffMonths实用方法,例如:

var query = query
            .Where(i=> 
                 EntityFunctions.DiffMonths(i.FromDate, i.ToDate) == 2
            );
Run Code Online (Sandbox Code Playgroud)

  • `EntityFunctions`现在已被'DbFunctions`取代 (5认同)