在使用LINQ到的DateTime实体 [英] Using DateTime in LINQ to Entities

查看:135
本文介绍了在使用LINQ到的DateTime实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过实体框架code首先程序交互的PostgreSQL数据库。

I have a PostgreSQL database that interacts with the program through Entity Framework Code First.

数据库包含表用户,他们拥有栏访问DateTime类型。

Database contains a table "users" that has column "visit" type of DateTime.

中的应用被描述为

public class Users
{    ...
    [Required]
    [Column("visit")]
    public DateTime VisitDate
    ...
}

我试图运行此查询;

I trying to run this query;

var rslt = context.Visitors.Where(v => v.VisitDate.Date == DateTime.Now.Date).ToList()

但是,让一个例外:引发NotSupportedException

怎么了?

推荐答案

DateTime.Date 属性。您必须使用 SqlFunctions.DatePart 方法来代替。这将结束与 DATEPART TSQL 生成的SQL查询中的方法。

DateTime.Date property is not supported. You have to use SqlFunctions.DatePart method instead. It will end up with DATEPART TSQL method within generated SQL query.

var rslt = context.Visitors
                  .Where(v => SqlFunctions.DatePart("year", v.VisitDate) == SqlFunctions.DatePart("year", DateTime.Now))
                  .Where(v => SqlFunctions.DatePart("dayofyear", v.VisitDate) == SqlFunctions.DatePart("dayofyear", DateTime.Now))
                  .ToList(); 

这篇关于在使用LINQ到的DateTime实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆