如何解决实体框架-Oracle-使用linq比较可为空的DateTime不起作用
我将Entity Framework与Oracle一起使用,并且尝试使用linq进行查询,比较可为空的DateTime,但是当我进行这种比较时,查询给我错误的结果。
我在PL / SQL Developer中执行了等效查询,并且得到了正确的结果。
这是我的代码
using (var context = new CorporativoContext())
{
context.Database.Log = Console.Write;
var parametro = (from p in context.PARAMETRO
join pv in context.PARAMETRO_VALOR on p.Id equals pv.Id
where p.Descricao == descricaoParametro && pv.InicioVigencia <= DateTime.Now && pv.FimVigencia == null
select pv).FirstOrDefault();
return parametro;
}
属性“ FimVigencia”是可为空的DateTime(DateTime?FimVigencia),当我将其与null进行比较时,查询结果将不返回任何内容,但是如果我删除此比较,查询将为我提供一个属性为“ FimVigencia”的值为空“。
有人可以帮助我吗?
解决方法
找到了解决方案。我将在这里发布任何有相同问题的人
我需要使用DbFunctions.TruncateTime来比较null
这是我的代码:
(from p in _context.PARAMETRO
join pv in _context.PARAMETRO_VALOR on p.Id equals pv.Id
where p.Descricao == descricaoParametro && pv.InicioVigencia <= DateTime.Now &&
(DbFunctions.TruncateTime(pv.FimVigencia) == null || pv.FimVigencia >= DateTime.Now)
select pv).FirstOrDefault();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。