0

我有以下模型:

public class FactCache
{
    public int ID { get; set; }
    public DateTime MonthDate { get; set; }
    public string AttributeKey { get; set; }

    public decimal RawValue { get; set; }
    public decimal ManDayValue { get; set; }
    public decimal FTEValue { get; set; }

    public int? InputResourceID { get; set; }
    public int? PhaseID { get; set; }

    public virtual InputResources InputResource { get; set; }
    public virtual DimPhase Phase { get; set; }
}

正如您在上面看到的,InputResourceID并且PhaseID是可为空的可选字段。

我想编写一个查询来查找给定日期的第一个条目,并且两者AttributeKey都为空。PhaseIDInputResourceID

我尝试了以下代码:

FactCache fact = db.FactCache.FirstOrDefault(
  a => a.InputResourceID == null
  && a.PhaseID == null
  && a.MonthDate == monthDate
  && a.AttributeKey == key);

但是,它返回一个空对象。编写上述查询的正确方法是什么?

我检查了数据库,确实有带有 nullPhaseIDInputResourceID.

4

2 回答 2

1

不确定,但也许...

FactCache fact = db.FactCache.FirstOrDefault(
  a => false == a.InputResourceID.HasValue
  && false == a.PhaseID.HasValue
  && a.MonthDate == monthDate
  && a.AttributeKey == key);
于 2013-05-15T15:46:53.707 回答
1

请注意,我很幸运能够运行 EF 版本 5 或更高版本,请尝试此处提出的解决方案:Call is failed with null parameter

于 2013-05-15T16:06:28.353 回答