相关疑难解决方法(0)

实体框架+ DayOfWeek

使用System.Linq.Dynamic(在这里管理https://github.com/kahanu/System.Linq.Dynamic),我试图使用Entity Framework 6(或更高版本)捕获在聚合目的DayOfWeek上找到的字段DateTime.

以前要求类似的东西,这有很大帮助,动态Linq +实体框架:动态选择的日期时间修改

实体框架支持DayOfWeek使用

SqlFunctions.DatePart("dw", datetime?)
Run Code Online (Sandbox Code Playgroud)

或者我们可以用类似的东西做一些更需要的东西

DbFunctions.DiffDays(date?, date?).  
Run Code Online (Sandbox Code Playgroud)

理念:

让Linq的DayOfWeek成为实体

我发现这非常有趣,我喜欢它,因为它不使用SqlFunctions可能让我局限于SQL Server的东西.此外,开发人员可以控制一周的第一天,而无需查询SQL Server属性以查找其配置方式(第一天).

出于实验目的,我一直试图在VisitMember()覆盖中实现这一点:

protected override Expression VisitMember(MemberExpression node)
{
     if (node.Type == typeof(System.DayOfWeek))
     {
          var firstSunday = new DateTime(1753, 1, 7);

                var firstSundayExpression = Expression.Constant(firstSunday, typeof(DateTime?));
                var timeValue = node.Expression;
                if (timeValue.Type != typeof(DateTime?)) timeValue = Expression.Convert(timeValue, typeof(DateTime?));
                var methodCall = Expression.Call(
                              typeof(DbFunctions), "DiffDays", Type.EmptyTypes, firstSundayExpression, timeValue);
                return Expression.Convert(methodCall, typeof(int?));
      } …
Run Code Online (Sandbox Code Playgroud)

c# linq expression-trees dynamic-linq entity-framework-6

4
推荐指数
1
解决办法
1820
查看次数